網(wǎng)頁爬蟲 - 關(guān)于python3.x 編寫爬蟲異常問題請各位大神幫忙!
問題描述
這段代碼一直出現(xiàn)異常,但只要把導(dǎo)入的庫稍加更改在python2.x中運(yùn)行是沒有問題的,請問大神問題出在哪?出現(xiàn)的異常也總變,小弟初學(xué)爬蟲,請賜教!
import urllib.errorimport urllib.requestimport urllib.parseurl = ’http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=https://www.baidu.com/link HTTP/1.1’data = {}data[’type’]= ’AUTO’data[’i’] = ’I am fine !’data[’doctype’] = ’json’data[’xmlVersion’] = ’1.8’data[’keyfrom’] = ’fanyi.web’data[’ue’] = ’UTF-8’data[’action’] = ’FY_BY_CLICKBUTTON’data[’typoResult’] = ’true’head = {}head[’User-Agent’]= ’Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0’ try: data = urllib.parse.urlencode(data).encode(’utf-8’) req = urllib.request.Request(url,data,head) response = urllib.request.urlopen(req) html = response.read().decode(’utf-8’) print(html)except urllib.error.HTTPError as e: print (’Error code : ’,e.code)except urllib.error.URLError as e: print (’The reason: ’,e.reason)
昨天是這樣的:
今天運(yùn)行就這樣了:
問題解答
回答1:刪掉url后面 HTTP/1.1,url后面加它干嘛。。

Python3不用解碼和編碼啦,還有些模塊的用法不一樣了,自己看一下Python3的文檔吧
回答3:
相關(guān)文章:
1. mac里的docker如何命令行開啟呢?2. 為什么我ping不通我的docker容器呢???3. nignx - docker內(nèi)nginx 80端口被占用4. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””5. docker容器呢SSH為什么連不通呢?6. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題7. angular.js使用$resource服務(wù)把數(shù)據(jù)存入mongodb的問題。8. javascript - 最近用echarts做統(tǒng)計(jì)圖時遇到兩個問題!!9. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個是怎么回事????10. docker gitlab 如何git clone?

網(wǎng)公網(wǎng)安備