码迷,mamicode.com
首页 > 编程语言 > 详细

Python爬虫实现翻译功能

时间:2020-11-24 12:19:27      阅读:8      评论:0      收藏:0      [点我收藏+]

标签:import   rom   准备   转换   pip   效果   client   oct   sig   

前言

学了这么久的python理论知识,需要开始实战来练手巩固了。

准备

首先安装爬虫urllib库

pip install urllib

获取有道翻译的链接url
技术图片
需要发送的参数在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)

运行会出现50的错误,这里需要将url链接的_o删除掉
技术图片
删除后运行成功
技术图片
但是这个结果看起来还是太复杂,需要在进行优化
导入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}‘)

优化完成,效果还行。
技术图片

Python爬虫实现翻译功能

标签:import   rom   准备   转换   pip   效果   client   oct   sig   

原文地址:https://www.cnblogs.com/huny/p/13997852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!