日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python爬虫运行正常最后报错_Python爬虫运行正常,最后却报错

發(fā)布時(shí)間:2024/9/18 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫运行正常最后报错_Python爬虫运行正常,最后却报错 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問題: Python爬蟲運(yùn)行正常,最后卻報(bào)錯(cuò)

描述:

剛學(xué)Python,不明白,特來請(qǐng)教。

程序是爬取貼吧某個(gè)地址的圖片,運(yùn)行正常,圖片也都下載下來了。

代碼如下:import?urllib.request

import?re

response?=?urllib.request.urlopen("http://tieba.baidu.com/p/3646792267?fr=ala0&pstaala=2&tpl=5")

html?=?response.read().decode("utf-8")

a='src="(.*?\.jpg)"'

c=re.findall(a,html)

s=0

for?i?in?c:

urllib.request.urlretrieve(i,"%s.png"?%?s)

s=s+1

然而在執(zhí)行完畢報(bào)錯(cuò):

Traceback?(most?recent?call?last):

File?"E:/untitled/getpng.py",?line?10,?in?

s=s+1

File?"E:\Python\lib\urllib\request.py",?line?187,?in?urlretrieve

with?contextlib.closing(urlopen(url,?data))?as?fp:

File?"E:\Python\lib\urllib\request.py",?line?162,?in?urlopen

return?opener.open(url,?data,?timeout)

File?"E:\Python\lib\urllib\request.py",?line?465,?in?open

response?=?self._open(req,?data)

File?"E:\Python\lib\urllib\request.py",?line?483,?in?_open

'_open',?req)

File?"E:\Python\lib\urllib\request.py",?line?443,?in?_call_chain

result?=?func(*args)

File?"E:\Python\lib\urllib\request.py",?line?1268,?in?http_open

return?self.do_open(http.client.HTTPConnection,?req)

File?"E:\Python\lib\urllib\request.py",?line?1240,?in?do_open

h.request(req.get_method(),?req.selector,?req.data,?headers)

File?"E:\Python\lib\http\client.py",?line?1083,?in?request

self._send_request(method,?url,?body,?headers)

File?"E:\Python\lib\http\client.py",?line?1118,?in?_send_request

self.putrequest(method,?url,?**skips)

File?"E:\Python\lib\http\client.py",?line?960,?in?putrequest

self._output(request.encode('ascii'))

UnicodeEncodeError:?'ascii'?codec?can't?encode?characters?in?position?263-268:?ordinal?not?in?range(128)

Process?finished?with?exit?code?1

大神解答一下這個(gè)是什么意思,萬分感謝。

解決方案1:

用?try?except?處理一下解決方案2:

把i打出來看看就知道了,因?yàn)橛袀€(gè).gif的圖片,你的正則匹配出來url就不對(duì)了,url里有中文,沒法用ascii編碼序列化.

最好不要用正則去解析DOM,寫個(gè)正確覆蓋各種邊邊角角情況的正則也不是那么容易的。

總結(jié)

以上是生活随笔為你收集整理的python爬虫运行正常最后报错_Python爬虫运行正常,最后却报错的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。