[Python] 纯文本查看 复制代码 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__')[0].string))
Translation=jsondata['props']['initialDvaState']['word']['wordInfo']['baesInfo']['translate_result']
print("翻译后的结果为:"+Translation)
|