html百度翻译界⾯,百度翻译界⾯测试(⼆),接⼝,2
经过
前⼀篇博客
的启发,⼜搞了⼀下,到了直接翻译的接⼝
import requests
import json
word = input("请输⼊你要查询的单词(汉译英):")
kv = {
"User-Agent":
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100
Safari/537.36",
# "Host":
# "fanyi.baidu",
# "Origin":
# "fanyi.baidu",
# "X-Requested-With":
# "XMLHttpRequest",
# "Referer":
# "fanyi.baidu/?aldtype=16047",
'Cookie':
'PSTM=1575539987; BAIDUID=A80872D93185A688C8AC8CF159A4EF54:FG=1;
BIDUPSID=3D8B9E5934B26CCEF003426B6773A431; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598;
H_PS_PSSID=1455_21114_26350_30717; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1;
HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; APPGUIDE_8_2_2=1;
from_lang_often=%5B%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value% to_lang_often=%5B%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value%22 Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1580901474,1580904731,1580905304,1580906217;
Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1580906217;
yjs_js_security_passport=0fdd751f0722a3d0fb6a389e7fe2c2f2c9864c58_1580906237_js;
__yjsv5_shitong=1.0_7_4a4fb445c2fea6132d704ca5e45285f82f61_300_1580906238325_223.91.220.158_37acdb4d;
delPer=0; PSINO=1'
}网页设计html代码翻译
form_data = {
'from': 'zh',
'to': 'en',
'query': word,
'transtype': 'realtime',
'simple_means_flag': '3',
'sign': '944849.690656',
'token': '42e08df4cfc832413f5c96516ed21cc3'
}
def getPages(url):
try:
response = requests.post(url, data=form_data, headers=kv)
response.raise_for_status()
t.decode()
except:
return "异常"
post_url = "fanyi.baidu/v2transapi?from=zh&to=en"
response = getPages(post_url)
# print(response)
result = json.loads(response)
# print(result)
print("“{}”的翻译成英⽂是:".format(word))
print(result["trans_result"]["data"][0]["dst"])
经过N次测试,headers除了要有User-Agent外,还要有Cookie, 但是也发现了⼀个问题——每⼀次查询时请求的表单⾥⾯的sign值都不⼀样,去掉这个参数也不⾏,唉,真是头疼!⽤上⾯的代码查询其他的句⼦就查询不出来了,⽬前还没出解决⽅法,有时间再试试吧

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。