Open the Developer Tools page translation in the proper way, find Request URL and the corresponding data in the Headers section.
Import urllib.request
Import urllib.parse
Import json
Content = the INPUT ( ' Please enter the content to be translated: ' )
# _O be removed, otherwise they will be the first error_code: error 50
url = ' http://fanyi.youdao.com/ Translate? smartresult = dict & smartresult = rule '
Data = {}
# developer tools there are, i and doctype key essential
Data [ ' I ' ] = Content
Data [ ' from ' ] = ' the AUTO '
Data [ ' to ' ] = ' AUTO '
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='15695569180611'
data['sign']='5b0565493d812bc5e713b895c12d615d'
data['doctype']='json'
data['version']='2.1'
data['keyfrom' ] = ' Fanyi.web '
Data [ ' Action ' ] = ' FY_BY_REALTTIME '
# the type of request dictionary data into url coding, coding type and converted to' utf-8 'type of
data = urllib.parse.urlencode ( data) .encode ( ' UTF-. 8 ' )
# request data in response to the result url
response = the urllib.request.urlopen (url, data)
# read return data content, decode the data is returned to the format conversion STR
HTML response = .read (). decode ( ' UTF-. 8 ' )
# use json strings to the dictionary
target = json.loads (HTML)
# results key in key 'translateResult' three-list = = 'tgt'In
Print (" Translation Results:% S " % (target [ ' translateResult ' ] [0] [0] [ ' TGT ' ]))
This might be the proper way above a lot to the page is not identified in the manual access, but access code.
At this point we can add ' the User-Agent ' agent. The purpose is achieved by setting the User Agent hide their identity, under normal circumstances is through the browser User-Agent to recognize.
UA set by calling urllib.request.Request ().
class urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None) This class is an abstraction of a URL request.
url should be a string containing a valid URL.
headers Should BE A #headers To the Dictionary Dictionary
There are two ways to set User Agent:
1. Request object when creating, fill in the headers parameter (includes User Agent information), the Headers parameter requirements for the dictionary;
2.在创建Request对象的时候不添加headers参数,在创建完成之后,使用add_header()的方法,添加headers。
import urllib.request
import urllib.parse
import json
import time
while True:
content=input('请输入需要翻译的内容(输入"q!退出程序"):')
if content=='q!':
break;
#_o要去掉,否则会出先error_code:50的报错
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
'''
第一种方法
head={}
head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
'''
data={}
#开发者工具里有,i和doctype键不可少
data['i']=content
data['from']='AUTO'
data['to']='AUTO'
data['smartresult']='dict'
data['client']='fanyideskweb'
data['salt']='15695569180611'
data['sign']='5b0565493d812bc5e713b895c12d615d'
data['doctype']='json'
data['version']='2.1'
data['keyfrom']='fanyi.web'
data['action']='FY_BY_REALTTIME'
#将字典类型的请求数据转化为url编码,并将编码类型转变为'utf-8'类型
data=urllib.parse.urlencode(data).encode('utf-8')
'''
第一种方法
req=urllib.request.Request(url,data,head)
'''
#第二种方法设置User Agent
#创建Request对象
req=urllib.request.Request(url,data)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36')
#传入创建好的Request对象
response=urllib.request.urlopen(req)
#读取响应信息并解码
html=response.read().decode('utf-8')
#使用json将字符串转化成字典
target=json.loads(html)
#结果在key='translateResult'的三层列表的key='tgt'中
print("翻译结果:%s" %(target['translateResult'][0][0]['tgt']))
#5秒一次
time.sleep(5)