标签:response 对象 链接 mda print ade https from head
说明:
1. 导入三个模块,urllib.request.urlopen用来打开url链接,urllib.parse的urlencode方法将浏览器network里的data对象转为urlopen的第二个参数data,json.loads()方法把response.read返回的字符串转为json格式。
2. data对象是从审查元素的network里的translate?开头的Headers里最下面的formdata里的所有属性。
3. urlopen(url,data)里的data要先encode编码成utf-8,得到返回值后的result要再decode解码回utf-8。
1 import urllib.request 2 import urllib.parse 3 import json 4 5 userinput = "" 6 while userinput != "exit": 7 userinput = input("输入要翻译的话(exit退出):") 8 url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=https://www.baidu.com/link" 9 data = {} 10 data["type"] = "AUTO" 11 data["i"] = userinput 12 data["doctype"] = "json" 13 data["xmlVersion"] = "1.8" 14 data["keyfrom"] = "fanyi.web" 15 data["ue"] = "UTF-8" 16 data["action"] = "FY_BY_CLICKBUTTON" 17 data["typoResult"] = "true" 18 data = urllib.parse.urlencode(data).encode("utf-8") 19 response = urllib.request.urlopen(url,data) 20 result = response.read().decode("utf-8") 21 result = json.loads(result) 22 result = result["translateResult"][0][0]["tgt"] 23 print(result)
标签:response 对象 链接 mda print ade https from head
原文地址:http://www.cnblogs.com/Jacklovely/p/6504780.html