網(wǎng)頁爬蟲 - 關于python3.x 編寫爬蟲異常問題請各位大神幫忙!
問題描述
這段代碼一直出現(xiàn)異常,但只要把導入的庫稍加更改在python2.x中運行是沒有問題的,請問大神問題出在哪?出現(xiàn)的異常也總變,小弟初學爬蟲,請賜教!
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)
昨天是這樣的:
今天運行就這樣了:
問題解答
回答1:刪掉url后面 HTTP/1.1,url后面加它干嘛。。
Python3不用解碼和編碼啦,還有些模塊的用法不一樣了,自己看一下Python3的文檔吧
回答3:相關文章:
1. javascript - 我的站點貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?2. [python2]local variable referenced before assignment問題3. html - 移動端radio無法選中4. php - 微信開發(fā)驗證服務器有效性5. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時間會消失是什么情況?6. javascript - 求幫助 , ATOM不顯示界面!!!!7. Python2中code.co_kwonlyargcount的等效寫法8. javascript - vue+iview upload傳參失敗 跨域問題后臺已經(jīng)解決 仍然報403,這是怎么回事啊?9. javascript - [MUI 子webview定位]10. mysql - 請問數(shù)據(jù)庫字段為年月日,傳進的參數(shù)為月,怎么查詢那個月所對應的數(shù)據(jù)
