第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

關(guān)于urllib.request.urlopen的編碼問題

python3.5.2,環(huán)境是pycharm

去掉了spider_main中的try-except后發(fā)現(xiàn)報(bào)錯(cuò)如下:

Traceback?(most?recent?call?last):
??File?"S:/Python?Learning/baike_spider/spider_main.py",?line?43,?in?<module>
????obj_spider.craw(root_url)??#?啟動(dòng)爬蟲
??File?"S:/Python?Learning/baike_spider/spider_main.py",?line?26,?in?craw
????html_cont?=?self.downloader.download(new_url)
??File?"S:\Python?Learning\baike_spider\html_downloader.py",?line?10,?in?download
????resp?=?request.urlopen(url)
??File?"E:\Tools\Python\Python3.5\lib\urllib\request.py",?line?163,?in?urlopen
????return?opener.open(url,?data,?timeout)
??File?"E:\Tools\Python\Python3.5\lib\urllib\request.py",?line?466,?in?open
????response?=?self._open(req,?data)
??File?"E:\Tools\Python\Python3.5\lib\urllib\request.py",?line?484,?in?_open
????'_open',?req)
??File?"E:\Tools\Python\Python3.5\lib\urllib\request.py",?line?444,?in?_call_chain
????result?=?func(*args)
??File?"E:\Tools\Python\Python3.5\lib\urllib\request.py",?line?1297,?in?https_open
????context=self._context,?check_hostname=self._check_hostname)
??File?"E:\Tools\Python\Python3.5\lib\urllib\request.py",?line?1254,?in?do_open
????h.request(req.get_method(),?req.selector,?req.data,?headers)
??File?"E:\Tools\Python\Python3.5\lib\http\client.py",?line?1107,?in?request
????self._send_request(method,?url,?body,?headers)
??File?"E:\Tools\Python\Python3.5\lib\http\client.py",?line?1142,?in?_send_request
????self.putrequest(method,?url,?**skips)
??File?"E:\Tools\Python\Python3.5\lib\http\client.py",?line?984,?in?putrequest
????self._output(request.encode('ascii'))
UnicodeEncodeError:?'ascii'?codec?can't?encode?characters?in?position?10-12:?ordinal?not?in?range(128)


html_downloader模塊代碼:

#?coding:utf-8
from?urllib?import?request
class?HtmlDownloader(object):
????def?download(self,?url):
????????if?url?is?None:
????????????return?None
????????resp?=?request.urlopen(url)
????????if?resp.getcode()?!=?200:
????????????return?None
????????return?resp.read()

查了很多但越改越亂,求解謝謝!

正在回答

2 回答

from?urllib?import?request
class?HtmlDownloader(object):
????def?download(self,?url):
????????if?url?is?None:
????????????return
????????
????????resp?=?request.urlopen(url)
????????
????????if?resp.getcode()?!=?200:
????????????return
????????
????????return?str(resp.read(),?encoding?=?'utf-8')

這樣試試?

0 回復(fù) 有任何疑惑可以回復(fù)我~

原視頻的講解是以Python2為例。如果是初學(xué),做實(shí)驗(yàn)時(shí)最好換成Python2,否則很多不兼容。

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
Python開發(fā)簡單爬蟲
  • 參與學(xué)習(xí)       227603    人
  • 解答問題       1288    個(gè)

本教程帶您解開python爬蟲這門神奇技術(shù)的面紗

進(jìn)入課程

關(guān)于urllib.request.urlopen的編碼問題

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)