|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
大家好,我现在在学习《零基础学python》的第十四章,论一只爬虫的自我修养。
在学习第14.2.2的时候遇到了一个问题,即“在编写爬去网易有道翻译结果的时候,书本中代码第三行的url应该怎么赋值?”
我的源代码:
import urllib.request
import urllib.parse
url="http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
data={}
data['type']='AUTO'
data['doctype']='json'
data['i']='i love fishc.com'
data['version']='2.1'
data['keyfrom']='fanyi.web'
data['ue']='UTF-8'
data['typoResult']='false'
data=urllib.parse.urlencode(data).encode('utf-8')
response=urllib.request.urlopen(url,data)
html=response.read().decode('utf-8')
print(html)
结果:{"errorCode":50}
上述编码中,除了URL不是按照课本上来以外,其他的都和课本一样。
有两个问题:
1、为什么我的url是用网页源代码上的url地址,结果会显示{"errorCode":50} ;
2、字典data中为什么要定义‘ue’,有什么作用?还有源代码的from data信息中,还有一些字段如client、sign、action这些为什么不用再字典中赋值?
书上讲的不清楚,请各位大佬抽空指点一下。感谢感谢!!
下面附上网页代码信息:
request header:
http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule
POST /translate_o?smartresult=dict&smartresult=rule HTTP/1.1
Host: fanyi.youdao.com
Connection: keep-alive
Content-Length: 217
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://fanyi.youdao.com
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://fanyi.youdao.com/?keyfrom=dict2.index
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Cookie: DICT_UGC=be3af0da19b5c5e6aa4e17bd8d90b28a|; OUTFOX_SEARCH_USER_ID=-1137543057@123.58.182.244; JSESSIONID=abcqd4Pc2Bhai-Ntqw5mw; OUTFOX_SEARCH_USER_ID_NCOO=1410678906.2771654; ___rl__test__cookies=1525677955507
|
-
网页源代码信息
|