1. 程式人生 > >簡單爬蟲之爬取網站圖片

簡單爬蟲之爬取網站圖片

 這裡選取的網址是  http://www.doutula.com   目的:爬取其中的圖片  並且翻頁爬取

首頁圖片的規則

<img src="https://ws3.sinaimg.cn/bmiddle/6af89bc8gw1f8oqmqwpjgj206o05k0ss.jpg" style="margin: 0px auto; min-height: inherit; height: 184.471px; display: block;" data-original="https://ws3.sinaimg.cn/bmiddle/6af89bc8gw1f8oqmqwpjgj206o05k0ss.jpg" alt="不發別走!" class="img-responsive lazy image_dta" data-backup="http://img.doutula.com/production/uploads/image//2016/04/27/20160427715202_Oaoikb.jpg!dta">

所以正則表示式

ImgUrlList=re.findall(r'<img src=.*?data-original="(.*?.jpg)"',htmltext)

怎麼實現翻頁功能呢,因為每一頁的連結有規律,So

ToUrl='http://www.doutula.com/article/list/?page='

思路就是:

1.列舉每個頁面 ,用正則表示式獲取其中的img的url和img的名字列表

2.對於每img根據其urll將之下載至電腦,用requests的get方法需要加上請求頭偽裝成該資訊是瀏覽器發出的,並且是該網頁請求 不然會被遮蔽請求

 

 

這裡有個坑點,其他頁面的img標籤與首頁的規則不同

 

<img class="lazy image_dtb img-responsive" src="https://ws2.sinaimg.cn/bmiddle/9150e4e5ly1fvscbyvi9mj206o06ot8n.jpg" data-original="https://ws2.sinaimg.cn/bmiddle/9150e4e5ly1fvscbyvi9mj206o06ot8n.jpg" data-backup="http://img.doutula.com/production/uploads/image//2018/10/11/20181011212059_SCoIQM.jpg!dta" alt="天鴨 - 鴨鴨表情" style="height: 170px; display: block;">

 所以我把正則表示式改為了

  ImgUrlList=re.findall(r'<img.*?src=.*?data-original="(.*?.jpg)"',htmltext)

 這樣就可以獲取每個頁面的img資訊(包括首頁)

import requests
import re
#UA卷則  代表請求由誰發出的
def Getimage(htmltext):#根據html程式碼   返回 圖片url列表和圖片名稱列表
    ImgUrlList=re.findall(r'<img.*?src=.*?data-original="(.*?.jpg)"',htmltext)#獲取img連結並且保證字尾為jpg
    ImgNameList=[]
    for url in ImgUrlList:
        url=url.split('/')
        ImgNameList.append(url[-1])
    return ImgUrlList,ImgNameList
def GetNexthtml(htmltext,url):#根據html程式碼 和總的url求出下一個next連結
    Nexthtml=re.findall(r'<a class="page-link" href="(/article/list/[?]page=\d+)',htmltext)
    print(Nexthtml)
    if len(Nexthtml)==0:
        raise Exception(u"don't next page  in GetNexthtml Fuction!")
    ToUrl=url+Nexthtml[0]
    return ToUrl
def Saveimg(Imgbit,Imgname):#根據二進位制檔案  和檔名字獲取圖片
    with open("imgs/{}".format(Imgname),"wb") as fp:
        fp.write(Imgbit)
RequestHeaders={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
    ,'Referer': 'http://www.doutula.com/'
}#前導空格不能有  此時是瀏覽器發出的
url="http://www.doutula.com"
nowurl=url
response = requests.get(nowurl, headers=RequestHeaders)  # 發起一個請求 得到html
if response.status_code!=200:
    raise  Exception(u"url沒有訪問許可權")
ImgUrlList,ImgNameList=Getimage(response.text)#根據htmltext得到 裡面url列表 和名字列表
count=len(ImgUrlList)
print(count)
for i in range(count):
    response=requests.get(ImgUrlList[i],headers=RequestHeaders)#圖片的響應頭的二進位制檔案在成員變數content裡面
    print(ImgUrlList[i],ImgNameList[i])
    Saveimg(response.content,ImgNameList[i])
ToUrl='http://www.doutula.com/article/list/?page='
for i in range(2,1000):#根據nowurl 得到請求
    nowurl="{}{}".format(ToUrl,str(i))
    print("nowurl:",nowurl)
    response = requests.get(nowurl, headers=RequestHeaders)  # 發起一個請求
    if response.status_code!=200:
        continue
    ImgUrlList,ImgNameList=Getimage(response.text)#根據html得到圖片列表
    count=len(ImgUrlList)
    print(count)
    for i in range(count):
        response=requests.get(ImgUrlList[i],headers=RequestHeaders)#圖片的響應頭的二進位制檔案在成員變數content裡面
        print(ImgUrlList[i],ImgNameList[i])
        Saveimg(response.content,ImgNameList[i])