spider_ use urllib Submit a post request, the proper way translation case

"" " 
Use urllib Submit a post request, the translation proper way
" ""

from urllib Import Request
from urllib Import the parse
Import JSON
URL = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
headers = { "the User-- Agent": "the Mozilla / 5.0 (the Windows NT 10.0; Win64; x64-; RV: 67.0) the Gecko / Firefox 20,100,101 / 67.0"}

# build parameters dictionary
parame = {
# translations for an
"I": "Hello" ,
"from": "AUTO" ,
"to": "AUTO" ,
"smartresult": "dict" ,
"Client":"fanyideskweb",
"salt":"15596318223592",
"sign":"6095c71447feda2d528eb238e957c476" ,
"TS": "1559631822359" ,
"BV": "e2a78ed30c66e16a857c5b6486a1d326" ,
"DOCTYPE": "json" ,
"Version": "2.1" ,
"keyfrom": "fanyi.web" ,
"Action": "FY_BY_REALTlME"
}

# parameters dictionary translated into strings
Data = parse.urlencode (parame) .encode ()
# dictionary parameter string containing entrained http request (POST), the return string
html = request.urlopen ( URL , Data = Data) .read (). decode ()
# Print (HTML)
# Print (type (HTML))

# json string into the file format, converting a string python dict objects,list
pythonObj=json.loads(html)
# print(pythonObj)
Print # (type (pythonObj))

# extract information
Result = pythonObj [ "translateResult"] [ 0] [ 0] [ 'TGT']
Print (Result)

Guess you like

Origin www.cnblogs.com/YangQingHong/p/10978193.html