学习爬虫有道翻译中遇到的

import urllib.request
import urllib.parse
import json
#from tkinter import *

'''root =Tk()
root.wm_attributes('-topmost',1)
root.geometry('+1000+40')
root.minsize(300,200)'''

fanyi = input('输入你想要翻译的:')

url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom="
#url中?前有个_o,删除后,response中的errorCode=50就会解决。

head = {}
head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36]'

data = {}
data['i']=fanyi
data['from']='AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='1507343425426'
data['sign']='0df813336ad806fbaa2e0120dddb1d02'
data['doctype']='json'
data['version']='2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_CLICKBUTTION'
data['typoResult']='true'

data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen(url,data)
html = response.read().decode('utf-8')
print (html)
target = json.loads(html)
result = target['translateResult'][0][0]['tgt']
print (result)

之前一直是print(response)处显示errorCode=50,百度之后听说是加密盐的问题,去搜索了关于salt和sign变量的算法,还是失败
告一段落之后,昨天发现了鱼C论坛以为网友做了一个tk的桌面翻译爬虫,查看源码之后发现只是把url中的_o去掉,就能用了。



你可能感兴趣的:(个人)