Python爬取套圖

爬取網址:www.mzitu.com

源代碼:


```python
import os
import requests
import bs4
from bs4 import BeautifulSoup
import random
import time



lots_headers = [
    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0",
    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)",
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
    'Opera/9.25 (Windows NT 5.1; U; en)',
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
    'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
    'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
    'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
    "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0",
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'
]
global headers
headers = {'User-Agent': random.choice(lots_headers)}

global savepath
savepath = "G:\ysltest2"

global mziTu
mziTu = "http://www.mzitu.com"


# 創建文件
def creatFile(filepath):
    if os.path.exists(filepath) is False:
        os.mkdir(filepath)
    os.chdir(filepath)


# 下載文件
def downLoad(file):
    global headers
    res_sub = requests.get(mziTu, headers=headers)
    soup_sub = BeautifulSoup(res_sub.text, 'html.parser')
    # 找到欄目
    all_a = soup_sub.find('div', class_='postlist').find_all('a', target='_blank')
    # 使用count防止重複下載
    count = 0
    for a in all_a:
        count = count + 1
        if count % 2 == 0:
            headers = {'User-Agent':random.choice(lots_headers)}
            # 提取href
            href = a.attrs['href']
            # print("套圖地址:"+href)
            res_sub_1 = requests.get(href, headers=headers)
            soup_sub_1 = BeautifulSoup(res_sub_1.text, 'html.parser')

            try:
                # 獲取最大套圖數量
                img_max = soup_sub_1.find('div', class_='pagenavi').find_all('span')[6].text
                # print("套圖數量:"+img_max)
                for j in range(1, int(img_max)+1):
                    time.sleep(random.randint(1, 3))
                    headers = {'User-Agent': random.choice(lots_headers)}
                    href_sub = href + "/" + str(j)
                    print("圖片地址:"+href_sub)
                    res_sub_2 = requests.get(href_sub, headers=headers)
                    soup_sub_2 = BeautifulSoup(res_sub_2.text, 'html.parser')
                    img = soup_sub_2.find('div', class_='main-image').find('img')
                    if isinstance(img, bs4.element.Tag):
                        # 提取src
                        src = img.attrs['src']
                        array = src.split('/')
                        file_name = array[len(array)-1]
                        # 防盜鏈加入Referer
                        headers = {'User-Agent': random.choice(lots_headers), 'Referer': src}
                        img = requests.get(src, headers=headers)
                        # print("開始下載圖片", img)
                        f = open(file_name, 'ab')
                        f.write(img.content)
                        print("圖片"+file_name+"保存成功,保存地址:"+file)
                        f.close()
            except Exception as e:
                print(e)


def main():
    res = requests.get(mziTu, headers=headers)
    soup = BeautifulSoup(res.text, 'html.parser')
    # 創建總文件夾
    creatFile(savepath)
    # 獲取首頁總頁數
    img_max = soup.find('div', class_='nav-links').find_all('a')[3].text
    print("總頁數爲"+img_max)
    for i in range(1, int(img_max)+1):
        # 獲取每頁的URL地址
        if i == 1:
            page = mziTu
        else:
            page = mziTu+'page/'+str(i)
        # 創建子文件夾
        file = savepath + "\\" + str(i)
        creatFile(file)
        # 下載圖片
        # print("套圖頁碼:"+page)
        downLoad(file)


if __name__ == '__main__':
    main()

``

示例截圖:

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章