Python抓取網(wǎng)頁圖片難點分析
一、網(wǎng)頁圖片抓取時代背景
隨著網(wǎng)絡技術的發(fā)展和互聯(lián)網(wǎng)的普及,由于網(wǎng)上用戶數(shù)量越來越龐大,網(wǎng)站同時并發(fā)的壓力比較大,尤其是大型網(wǎng)站,因此現(xiàn)在網(wǎng)頁圖片都采取懶加載(Lazy Load)的方式;還出現(xiàn)了好多為了采集資源而出現(xiàn)的網(wǎng)絡爬蟲(Net spider),為了反制圖片爬蟲,研發(fā)人員都不會把網(wǎng)頁的圖片地址放到<image>標簽的src屬性中去,而放到其他屬性中去通過腳本來異步加載,或者頁面中根本沒有圖片地址,通過專門的異步請求來單獨獲取和處理,還有就是針對頻繁下載IP進行封號。
二、網(wǎng)頁圖片抓取難點處理
1、圖片地址存放位置不同
采用以下網(wǎng)頁圖片抓取代碼,把存放到不同位置的圖片地址都抓取出來,:
# 獲取圖片的真實地址
if img_node_type==0:
fileUrl=''
# 優(yōu)先src之外的其他屬性,這些屬性一般存放真實地址,發(fā)爬都是這樣設計的
for attrkey in img.attrs:
if attrkey=='src':
continue
tempurl = str(img.attrs[attrkey])
tempurl=get_imageurl_in_str(tempurl)
if tempurl=="": #當前屬性值不含圖片url則繼續(xù)搜索其他屬性值
continue
if tempurl[:4]=="http":
fileUrl = tempurl
elif tempurl[:2]=="http://":
fileUrl = "http:"+tempurl
elif tempurl[:1] == "/": # 當前頁面地址作為路徑
fileUrl = get_root_by_pageurl(pageUrl) + tempurl
elif tempurl[:2] == "./": # 當前頁面地址作為路徑
fileUrl = get_root_by_pageurl(pageUrl) + tempurl[1:]
elif tempurl[:3] == "../": # 當前頁面地址作為路徑
fileUrl = get_root_by_pageurl(pageUrl) + tempurl[2:]
if fileUrl!='':
break
# 如果在其他屬性未能找到圖片的真實地址,則再在src屬性尋找
if fileUrl=='': #圖片地址為空則不下載
for attrkey in img.attrs:
if attrkey=='src':
tempurl = str(img.attrs[attrkey])
tempurl=get_imageurl_in_str(tempurl)
if tempurl=="": #當前屬性值不含圖片url則繼續(xù)搜索其他屬性值
continue
if tempurl[:4]=="http":
fileUrl = tempurl
elif tempurl[:2]=="http://":
fileUrl = "http:"+tempurl
elif tempurl[:1] == "/": # 當前頁面地址作為路徑
fileUrl = get_root_by_pageurl(pageUrl) + tempurl
elif tempurl[:2] == "./": # 當前頁面地址作為路徑
fileUrl = get_root_by_pageurl(pageUrl) + tempurl[1:]
elif tempurl[:3] == "../": # 當前頁面地址作為路徑
fileUrl = get_root_by_pageurl(pageUrl) + tempurl[2:]
if fileUrl!='':
break2、圖片地址格式解析處理
采用以下網(wǎng)頁圖片抓取代碼,可以從紛繁復雜的文本中把正確的圖片地址提取處理來:
# 按照正則方式進行匹配查找類似'http://*.jpg,https://*.jpg'
list2_jpg = re.findall(r"http(.+?)\.jpg", pageUrl)
list2_png = re.findall(r"http(.+?)\.png", pageUrl)
list2 = []
for m in range(len(list2_jpg)):
while len(re.findall(r"http(.+?)\.jpg", list2_jpg[m] + ".jpg")) > 0:
list2_jpg[m] = re.findall(r"http(.+?)\.jpg", list2_jpg[m] + ".jpg")[0]
list2_jpg[m] = list2_jpg[m].replace("\\", "") # 去掉轉(zhuǎn)義反斜杠
list2_jpg[m] = unquote(unquote(list2_jpg[m])) # 去掉Hex字符,類似%3A%2F%2F 應為://
list2.append("http" + list2_jpg[m] + ".jpg")
for m in range(len(list2_png)):
while len(re.findall(r"http(.+?)\.png", list2_png[m] + ".png")) > 0:
list2_png[m] = re.findall(r"http(.+?)\.png", list2_png[m] + ".png")[0]
list2_png[m] = list2_png[m].replace("\\", "") # 去掉轉(zhuǎn)義反斜杠
list2_png[m] = unquote(unquote(list2_png[m])) # 去掉Hex字符,類似%3A%2F%2F 應為://
list2.append("http" + list2_png[m] + ".png")3、防止IP被封可以采用代理Ip機制
采用以下網(wǎng)頁圖片抓取代碼,可以防止本機IP被封:
try:
# response=requests.request('GET', pageUrl, headers=headers, proxies=ProxyPool.get_Proxy_Str(poolName), verify=False)
response=requests.request('GET', pageUrl, headers=headers, timeout=5) #, proxies=ProxyPool.get_Proxy_Str(poolName), verify=False)
except Exception as e:
my_logger_debug(logger,"local request page fail,page url:[%s] " % (pageUrl))
try: # 反爬蟲:先使用本機,不行再使用代理IP
proxy_ip_mode = int(proxyiper.getValueByKey('mode'))
if proxy_ip_mode == 0: # 調(diào)用代理Ip API 不同用戶配置不同,按使用代理IP數(shù)量收費
response=requests.request('GET', pageUrl, headers=headers, timeout=5, proxies=ProxyPool.get_Proxy_Str(poolName), verify=False)
elif proxy_ip_mode == 1: # 使用ADSL服務器動態(tài)切換IP,按照租賃ADSL服務器收費
switchIp(logger)
response = requests.request('GET', pageUrl, headers=headers,
timeout=5) # , proxies=ProxyPool.get_Proxy_Str(poolName), verify=False)
except Exception as e:
my_logger_debug(logger, e)
my_logger_debug(logger,"proxy request page fail,page url:[%s]" % (pageUrl))
return三、網(wǎng)頁圖片抓取場景分類
基本現(xiàn)在的網(wǎng)頁圖片抓取場景可以分為2種:
場景1:原來從各大搜索引擎(例如百度、360、搜狐等)和知名圖片網(wǎng)站(昵圖網(wǎng)、匯圖網(wǎng)等),輸入圖片關鍵詞進行搜索,然后一頁一頁翻看圖片搜索結(jié)果,現(xiàn)在想在下載工具上輸入圖片關鍵字,一鍵把圖片搜索結(jié)果下載到本地。
場景2:從指定的網(wǎng)站首頁或網(wǎng)站內(nèi)頁(包括單個頁面或多個頁面)上一鍵下載圖片到本地計算機上,如果要把某個網(wǎng)站所有頁面上的圖片都下載下來,可以先使用類似SiteMap X這種網(wǎng)站地圖掃描工具,然后再把地址文件導入圖片抓取工具來下載。Demo示例截圖如下:、
有相關經(jīng)驗或碰到類似問題的同學,可以后臺評論區(qū)留言大家一起交流討論。
到此這篇關于Python抓取網(wǎng)頁圖片難點分析的文章就介紹到這了,更多相關Python抓取網(wǎng)頁圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決pymysql cursor.fetchall() 獲取不到數(shù)據(jù)的問題
這篇文章主要介紹了解決pymysql cursor.fetchall() 獲取不到數(shù)據(jù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
使用Python中的cookielib模擬登錄網(wǎng)站
這篇文章主要介紹了使用Python中的cookielib模擬登錄網(wǎng)站,用作生成cookie然后登錄,需要的朋友可以參考下2015-04-04
Python3 多線程(連接池)操作MySQL插入數(shù)據(jù)
本文將結(jié)合實例代碼,介紹Python3 多線程(連接池)操作MySQL插入數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
Python?matplotlib繪圖時使用鼠標滾輪放大/縮小圖像
Matplotlib是Python程序員可用的事實上的繪圖庫,雖然它比交互式繪圖庫在圖形上更簡單,但它仍然可以一個強大的工具,下面這篇文章主要給大家介紹了關于Python?matplotlib繪圖時使用鼠標滾輪放大/縮小圖像的相關資料,需要的朋友可以參考下2022-05-05
Python實現(xiàn)求取表格文件某個區(qū)域內(nèi)單元格的最大值
這篇文章主要介紹基于Python語言,基于Excel表格文件內(nèi)某一列的數(shù)據(jù),計算這一列數(shù)據(jù)在每一個指定數(shù)量的行的范圍內(nèi)(例如每一個4行的范圍內(nèi))的區(qū)間最大值的方法,需要的朋友可以參考下2023-08-08

