|
发表于 2020-6-4 16:30:31
|
显示全部楼层
- import urllib.request
- import urllib.parse #由于需要输入符合urlopen函数中参数data的格式而引入
- import json
- content = input("请输入要翻译的内容:")
- url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc'
- data = {
- "type" : "AUTO",
- "i" : 'sentence',
- "doctype" : "json",
- "xmlVersion" : "1.8",
- "keyfrom" : "fanyi.web",
- "ue" : "UTF-8",
- "action" : "FY_BY_CLICKBUTTON",
- "typoResult" : "true" }
- data = urllib.parse.urlencode(data).encode("utf-8") #将Unicode编码转换成utf-8编码格式
- response = urllib.request.urlopen(url,data)
- html = response.read().decode("utf-8") #response.read()后是utf-8编码格式的文件,将utf-8编码转换成Unicode编码格式
- target = json.loads(html)
复制代码
- 请输入要翻译的内容:>? 我爱你
- 翻译结果为:您的请求来源非法,商业用途使用请关注有道翻译API官方网站“有道智云”: http://ai.youdao.com
复制代码
现在换成你写的那样也不行了 只能用反爬了 |
|