vlambda博客
学习文章列表

网页爬虫实践——有道翻译

# author:sunbaogang
# Email:[email protected]
# date created:2022418
# 有道翻译我爱数据网址:http://fanyi.youdao.com 相对于chrome浏览器开发者工具DevTools,在Form_data中增加了'typoResult':'false'

#Form_data中的变量都加了‘’号,DevTools中没有,所以需要手动加入。以下代码是用python实现

import requests
import json
def get_translate_date(word=None):
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
Form_data = {'i':word, 'from':'AUTO','to': 'AUTO','smartresult': 'dict', 'client':'fanyideskweb',
'salt':'16502642147813','sign':'252cc0e33d022a2c6bb9d4fa25064cf5','doctype':'json',
'version': '2.1','keyfrom':'fanyi.web','action':'FY_BY_CLICKBUTTION','typoResult':'false'}
response = requests.post(url, data=Form_data) # 请求表单数据
content = json.loads(response.text) # JSON格式字符串转字典
print(content['translateResult'][0][0]['tgt']) # 打印翻译后的数据
if __name__ == '__main__':
get_translate_date('我爱数据')