cdsgg 发表于 2021-4-30 14:52

送你们一个免费的翻译接口 甚至于都不用js逆向

import json
import requests
from bs4 import BeautifulSoup

worlds=input('请输入您要翻译的语句:')


url=f'https://www.iciba.com/word?w={worlds}'

headers={
"Cookie": "BAIDU_SSP_lcr=https://www.baidu.com/link?url=h41ZWC_v-xcb8WZpDtq9eRN90gb1nTCMttJHlqLwWh_&wd=&eqid=8317b8ab000e8cf100000004607026b2",
"Host": "www.iciba.com",
"Pragma": "no-cache",
"Referer": "https://www.iciba.com/word?w=%E5%A5%BD%E6%83%B3%E4%BD%A0",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0"
}

r=requests.get(url,headers=headers).content.decode()
jsondata=json.loads(str(BeautifulSoup(r,'lxml').select('#__NEXT_DATA__').string))
Translation=jsondata['props']['initialDvaState']['word']['wordInfo']['baesInfo']['translate_result']

print("翻译后的结果为:"+Translation)

so_so_so 发表于 2021-4-30 15:12

本帖最后由 so_so_so 于 2021-4-30 15:14 编辑

jsondata = json.loads(str(BeautifulSoup(r, 'lxml').select('#__NEXT_DATA__')[0].string))报错:bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?应该是需要安装:pips install lxml安装不上,修改:jsondata = json.loads(str(BeautifulSoup(r, 'html.parser').select('#__NEXT_DATA__').string))

952712345 发表于 2021-4-30 15:59

data = {
      'doctype': 'json',
      'type': 'AUTO',
      'i': plainText
    }
    url = "http://fanyi.youdao.com/translate"
    r = requests.get(url, params=data)
    result = r.json()

这个不是更方便!!!!

狼灭大帝 发表于 2021-4-30 15:00

大佬牛啵一

人称昊哥 发表于 2021-4-30 15:04

感谢大佬的分享啊,很有帮助

ff5500 发表于 2021-4-30 15:10

有用啊!收下了!谢谢!!

cdsgg 发表于 2021-4-30 15:13

so_so_so 发表于 2021-4-30 15:12
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need ...

pip install lxml

so_so_so 发表于 2021-4-30 15:15

cdsgg 发表于 2021-4-30 15:13
pip install lxml

谢谢分享。

我是一个外星人 发表于 2021-4-30 15:17

感谢分享。

m58758788 发表于 2021-4-30 15:50

接口应该是类似这样的吧https://dict.iciba.com/dictionary/word/suggestion?word=good

lendone 发表于 2021-4-30 15:52

https://p.pstatp.com/origin/pgc-image/39939ef251d7486fadeedb32c30a5f3b 亲测可用
页: [1] 2
查看完整版本: 送你们一个免费的翻译接口 甚至于都不用js逆向