網頁爬蟲 - 關于python3.x 編寫爬蟲異常問題請各位大神幫忙!
問題描述
這段代碼一直出現異常,但只要把導入的庫稍加更改在python2.x中運行是沒有問題的,請問大神問題出在哪?出現的異常也總變,小弟初學爬蟲,請賜教!
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. mac OSX10.12.4 (16E195)下Mysql 5.7.18找不到配置文件my.cnf2. mysql - 怎么生成這個sql表?3. mysql儲存json錯誤4. php - 公眾號文章底部的小程序二維碼如何統計?5. mysql - 表名稱前綴到底有啥用?6. mysql - 數據庫表中,兩個表互為外鍵參考如何解決7. Navicat for mysql 中以json格式儲存的數據存在大量反斜杠,如何去除?8. 在mybatis使用mysql的ON DUPLICATE KEY UPDATE語法實現存在即更新應該使用哪個標簽?9. mysql - 數據庫建字段,默認值空和empty string有什么區別 11010. sql語句 - 如何在mysql中批量添加用戶?
