網(wǎng)頁(yè)爬蟲 - 關(guān)于python3.x 編寫爬蟲異常問題請(qǐng)各位大神幫忙!
問題描述
這段代碼一直出現(xiàn)異常,但只要把導(dǎo)入的庫(kù)稍加更改在python2.x中運(yùn)行是沒有問題的,請(qǐng)問大神問題出在哪?出現(xiàn)的異常也總變,小弟初學(xué)爬蟲,請(qǐng)賜教!
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. node.js - nodejs debug問題2. docker-machine添加一個(gè)已有的docker主機(jī)問題3. golang - 用IDE看docker源碼時(shí)的小問題4. docker綁定了nginx端口 外部訪問不到5. docker - 如何修改運(yùn)行中容器的配置6. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?7. debian - docker依賴的aufs-tools源碼哪里可以找到啊?8. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個(gè)是怎么回事????9. docker網(wǎng)絡(luò)端口映射,沒有方便點(diǎn)的操作方法么?10. docker-compose中volumes的問題
