需求一:通过urllib获取有道翻译。
注意:
1.urllib.parse.urlencode( ),将字典里面所有的键值转化为格式 (key=value&key=value),并且将中文转码(如:KW=%E9%B1%BC&)。
2.bytes( ),所有的post请求提交的数据类型都是字节型数据。
3.响应返回的是json,是Response Header里的Content-Type;application/json; charset=utf-8的原因吗。
4.headers视情况传入。有时headers还必须包含Cookie。
import urllib.request
import urllib.parse
import json
content = input("输入需要翻译的内容:")
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data = {
"i": content,"from": "AUTO","to":"AUTO","smartresult":"dict","client":"fanyideskweb","salt":