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!='': break
2、圖片地址格式解析處理
采用以下網(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)等),輸入圖片關鍵詞進行搜索,然后一頁一頁翻看圖片搜索結果,現(xiàn)在想在下載工具上輸入圖片關鍵字,一鍵把圖片搜索結果下載到本地。
場景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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
深度學習Tensorflow2.8實現(xiàn)GRU文本生成任務詳解
這篇文章主要為大家介紹了深度學習Tensorflow?2.8?實現(xiàn)?GRU?文本生成任務示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01利用django和mysql實現(xiàn)一個簡單的web登錄頁面
這篇文章主要給大家介紹了關于如何利用django和mysql實現(xiàn)一個簡單的web登錄頁面的相關資料,文中通過圖文以及實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2023-05-05