python爬虫---爬取有道翻译

这两天在学习爬虫,老师让我破解有道翻译的反爬虫机制爬取内容。话不多说直接上代码

from  urllib import request,parse
import time,json,random,hashlib


content=input("请输入要翻译的内容:")
base_url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
#Request URL:http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule  注释内的有个_o,
# 如果不删除会显示errorCode=50,并不会给出翻译结果。删除就解决了

data={} #定义一个字典用来接收传给服务器的内容

u = 'fanyideskweb'
d = content
f = str(int(time.time()*1000) + random.randint(1,10))
c = 'rY0D^0\'nM0}g5Mm1z%1G4'

sign=hashlib.md5((u+d+f+c).encode('utf-8')).hexdigest()
#引号内容为固定
data['i']=content#需要翻译的内容
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = f
data['sign'] = sign
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_CL1CKBUTTON'
data['typoResult'] = 'true'

data = parse.urlencode(data).encode('utf-8')
req = request.Request(base_url,data=data)
response = request.urlopen(req)

res=response.read().decode('utf-8')
res=json.loads(res)
res=res["translateResult"]
print(res)




你可能感兴趣的:(python基础)