关于urllib模块运行问题
# -*- coding:utf-8 -*-import urllib.request
import urllib.parse
import json
data='''i:爱情
from:AUTO
to:AUTO
smartresult:dict
client:fanyideskweb
salt:1493968599235
sign:bb13dce52ebe3879e98bebf98afed439
doctype:json
version:2.1
keyfrom:fanyi.web
action:FY_BY_ENTER
typoResult:true'''
a=data.split('\n')
b={}
for erch in a:
c=erch.split(':')
b]=c
data=b
data['i']=input('请输入要翻译的内容:')
data=urllib.parse.urlencode(data).encode('utf-8')
url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom=null'
response=urllib.request.urlopen(url)
html=response.read().decode('utf-8')
print(html)
上面是我的代码
>>>
请输入要翻译的内容:hello
{"errorCode":50}
这是运行结果,搞不明白出了什么问题,请教大神了 有道好像有反爬虫机制,要加上headers属性伪装成浏览器 新房客 发表于 2017-5-5 17:44
有道好像有反爬虫机制,要加上headers属性伪装成浏览器
#!/usr/bin/python
#-*- coding:utf-8 -*-
import urllib.request
import urllib.parse
url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom=null'
head={'Connection':'keep-alive','Referer':'http://fanyi.youdao.com/',
'User-Agent':'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1'}
a='''i:爱情
from:AUTO
to:AUTO
smartresult:dict
client:fanyideskweb
salt:1493980359397
sign:30ebd3645a6256a0fb9d48ab73041355
doctype:json
version:2.1
keyfrom:fanyi.web
action:FY_BY_CLICKBUTTON
typoResult:true'''
b=a.split('\n')
data={}
for each in b:
c=each.split(':')
data]=c
data['i']=input('请输入要翻译的内容:')
data=urllib.parse.urlencode(data).encode('utf-8')#将date转化为utf-8格式
req=urllib.request.Request(url,data=data,headers=head)
response=urllib.request.urlopen(req)
html = response.read().decode('utf-8')
print(html)
这是我加了以后的代码,结果还是一样 chyiso 发表于 2017-5-5 19:12
#!/usr/bin/python
#-*- coding:utf-8 -*-
import urllib.request
response=urllib.request.urlopen(url,data) 我觉得是你的地址有误{:10_245:}
你的地址:url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom=null'
爬取用的地址:url="http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null"
我自己也不知道为什么{:10_266:} 可参考这个帖子,希望能帮到你
http://bbs.fishc.com/forum.php?mod=viewthread&tid=86581&page=1#pid2877875 本帖最后由 lumber2388779 于 2017-5-6 00:41 编辑
http://fanyi.youdao.com/openapi?path=data-mode
你的key有错误的
50 - 无效的key
仔细查了下
发现你的
url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom=null'
改成就可以了
url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
这个应该是现在有道词典自己做了保护导致这个检查出来跟实际有出入
页:
[1]