Python在網(wǎng)站爬取所有圖片鏈接并判斷其狀態(tài)值

#coding:utf-8

import urllib

from bs4 import BeautifulSoup

import urllib2

for pa in range(1,200):

url = "http://www.xxxxx.com/page/{}".format(pa)

html = urllib.urlopen(url).read()

soup = BeautifulSoup(html, "html.parser")

imglist = soup.find_all("img")

lenth = len(imglist)

for i in range(lenth):

Pictu = imglist[i].attrs['src']

req = urllib2.Request(Pictu)

response = None

try:

response = urllib2.urlopen(req, timeout=5)

print response.geturl()

print response.getcode()

except urllib2.URLError as e:

print e

if hasattr(e, 'code'):

print e.geturl()

print 'Error code:', e.code

print e.info

elif hasattr(e, 'reason'):

print 'Reason:', e.reason

except:

pass

finally:

if response:

response.close()


初次編寫此代碼,實現(xiàn)在網(wǎng)站爬取所有圖片(翻頁),并判斷其狀態(tài)值,若有錯誤,歡迎指正!


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容