123666666 发表于 2020-12-21 22:38:04

爬虫实战那一讲,能·告诉·我错在哪吗?



import urllib.request
import urllib.parse


url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'


data={}
data['type']='AUTO'
data['i']='I love you'
data['doctype']= 'json'
data['xmlVersion']= '2.1'
data['keyfrom']=' fanyi.web'
data['ue']='UTF-8'
data['typoResult']='true'

data= urllib.parse.urlencode(data).encode('utf-8')



response=urllib.request.urlopen(url,data)
html=response.read().decode('utf-8')

print(html)
结果
{"errorCode":50}
我连错误是什么都不懂

逃兵 发表于 2020-12-22 17:12:50

视频是很久前的了,有道加了一些反爬机制
url里的'_o'去掉
data去掉一些键(data['i']='I love you' ,data['doctype']= 'json'不能去)
建议还是学一下requests模块

import urllib.request
import urllib.parse


url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'

data={}
data['type']='AUTO'
data['i']='I love you'
data['doctype']= 'json'
data['ue']='UTF-8'


data= urllib.parse.urlencode(data).encode('utf-8')



response=urllib.request.urlopen(url,data)
html=response.read().decode('utf-8')

print(html)

123666666 发表于 2020-12-23 19:58:30

逃兵 发表于 2020-12-22 17:12
视频是很久前的了,有道加了一些反爬机制
url里的'_o'去掉
data去掉一些键(data['i']='I love you' ,data ...

谢谢大佬
页: [1]
查看完整版本: 爬虫实战那一讲,能·告诉·我错在哪吗?