Python爬虫实现翻译功能
2021-03-15 07:29
标签:import rom 准备 转换 pip 效果 client oct sig 学了这么久的python理论知识,需要开始实战来练手巩固了。 首先安装爬虫urllib库 获取有道翻译的链接url 运行会出现50的错误,这里需要将url链接的_o删除掉 优化完成,效果还行。 Python爬虫实现翻译功能 标签:import rom 准备 转换 pip 效果 client oct sig 原文地址:https://www.cnblogs.com/huny/p/13997852.html前言
准备
pip install urllib
需要发送的参数在form data里示例
import urllib.request
import urllib.parse
url = ‘http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule‘
data = {}
data[‘i‘] = ‘i love python‘
data[‘from‘] = ‘AUTO‘
data[‘to‘] = ‘AUTO‘
data[‘smartresult‘] = ‘dict‘
data[‘client‘] = ‘fanyideskweb‘
data[‘salt‘] = ‘16057996372935‘
data[‘sign‘] = ‘0965172abb459f8c7a791df4184bf51c‘
data[‘lts‘] = ‘1605799637293‘
data[‘bv‘] = ‘f7d97c24a497388db1420108e6c3537b‘
data[‘doctype‘] = ‘json‘
data[‘version‘] = ‘2.1‘
data[‘keyfrom‘] = ‘fanyi.web‘
data[‘action‘] = ‘FY_BY_REALTlME‘
data = urllib.parse.urlencode(data).encode(‘utf-8‘)
response = urllib.request.urlopen(url,data)
html = response.read().decode(‘utf-8‘)
print(html)
删除后运行成功
但是这个结果看起来还是太复杂,需要在进行优化
导入json,然后转换成字典进行过滤import urllib.request
import urllib.parse
import json
url = ‘http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule‘
data = {}
data[‘i‘] = ‘i love python‘
data[‘from‘] = ‘AUTO‘
data[‘to‘] = ‘AUTO‘
data[‘smartresult‘] = ‘dict‘
data[‘client‘] = ‘fanyideskweb‘
data[‘salt‘] = ‘16057996372935‘
data[‘sign‘] = ‘0965172abb459f8c7a791df4184bf51c‘
data[‘lts‘] = ‘1605799637293‘
data[‘bv‘] = ‘f7d97c24a497388db1420108e6c3537b‘
data[‘doctype‘] = ‘json‘
data[‘version‘] = ‘2.1‘
data[‘keyfrom‘] = ‘fanyi.web‘
data[‘action‘] = ‘FY_BY_REALTlME‘
data = urllib.parse.urlencode(data).encode(‘utf-8‘)
response = urllib.request.urlopen(url,data)
html = response.read().decode(‘utf-8‘)
req = json.loads(html)
result = req[‘translateResult‘][0][0][‘tgt‘]
print(result)
但是这个程序只能翻译一个单词,用完就废了。于是我在进行优化import urllib.request
import urllib.parse
import json
centens = input(‘输入要翻译的语句:‘)
url = ‘http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule‘
data = {}
data[‘i‘] = centens
data[‘from‘] = ‘AUTO‘
data[‘to‘] = ‘AUTO‘
data[‘smartresult‘] = ‘dict‘
data[‘client‘] = ‘fanyideskweb‘
data[‘salt‘] = ‘16057996372935‘
data[‘sign‘] = ‘0965172abb459f8c7a791df4184bf51c‘
data[‘lts‘] = ‘1605799637293‘
data[‘bv‘] = ‘f7d97c24a497388db1420108e6c3537b‘
data[‘doctype‘] = ‘json‘
data[‘version‘] = ‘2.1‘
data[‘keyfrom‘] = ‘fanyi.web‘
data[‘action‘] = ‘FY_BY_REALTlME‘
data = urllib.parse.urlencode(data).encode(‘utf-8‘)
response = urllib.request.urlopen(url,data)
html = response.read().decode(‘utf-8‘)
req = json.loads(html)
result = req[‘translateResult‘][0][0][‘tgt‘]
print(f‘中英互译的结果:{result}‘)