欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Python爬取妹子圖

系統 1734 0

爬蟲成果

Python爬取妹子圖_第1張圖片
當你運行代碼后,文件夾就會越來越多,如果爬完的話會有2000多個文件夾,20000多張圖片。不過會很耗時間,可以在最后的代碼設置爬取頁碼范圍。

本文目標

  • 熟悉 Requests 庫,
  • Beautiful Soup 庫 熟悉多線程爬取
  • 送福利,妹子圖

網站結構

我們從 http://meizitu.com/a/more_1.html 這個鏈接進去,界面如圖一所示

圖一:
Python爬取妹子圖_第2張圖片
可以看到是一組一組的套圖,點擊任何一組圖片會進入到詳情界面,如圖二所示

圖二:

可以看到圖片是依次排開的,一般會有十張左右的圖片。

實現思路

看了界面的結構,那么我們的思路就有了。

  1. 構造 url 鏈接,去請求圖一所示的套圖列表界面,拿到每一個頁面中的套圖列表。
  2. 分別進入每個套圖中去,下載相應的圖片。

代碼說明

1、下載界面的函數,利用 Requests 很方便實現。

            
              def download_page(url):
    '''
    用于下載頁面
    '''
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"}
    r = requests.get(url, headers=headers)
    r.encoding = 'gb2312'
    return r.text

            
          

2、獲取圖一所示的所有套圖列表,函數中 link 表示套圖的鏈接,text表示套圖的名字

            
              def get_pic_list(html):
    '''
    獲取每個頁面的套圖列表,之后循環調用get_pic函數獲取圖片
    '''
    soup = BeautifulSoup(html, 'html.parser')
    pic_list = soup.find_all('li', class_='wp-item')
    for i in pic_list:
        a_tag = i.find('h3', class_='tit').find('a')
        link = a_tag.get('href')  # 套圖鏈接
        text = a_tag.get_text()   # 套圖名字
        get_pic(link, text)

            
          

3、傳入上一步中獲取到的套圖鏈接及套圖名字,獲取每組套圖里面的圖片,并保存,我在代碼中注釋了。

            
              def get_pic(link, text):
    '''
    獲取當前頁面的圖片,并保存
    '''
    html = download_page(link)  # 下載界面
    soup = BeautifulSoup(html, 'html.parser')
    pic_list = soup.find('div', id="picture").find_all('img')  # 找到界面所有圖片
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"}
    create_dir('pic/{}'.format(text))
    for i in pic_list:
        pic_link = i.get('src')  # 拿到圖片的具體 url
        r = requests.get(pic_link, headers=headers)  # 下載圖片,之后保存到文件
        with open('pic/{}/{}'.format(text, pic_link.split('/')[-1]), 'wb') as f:
            f.write(r.content)
            time.sleep(1)

            
          

完整代碼

完整代碼如下,包括了創建文件夾,利用多線程爬取,我設置的是5個線程,可以根據自己機器自己來設置一下。

            
              import requests
import os
import time
import threading
from bs4 import BeautifulSoup
'''
遇到不懂的問題?Python學習交流群:821460695滿足你的需求,資料都已經上傳群文件,可以自行下載!
'''

def download_page(url):
    '''
    用于下載頁面
    '''
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"}
    r = requests.get(url, headers=headers)
    r.encoding = 'gb2312'
    return r.text


def get_pic_list(html):
    '''
    獲取每個頁面的套圖列表,之后循環調用get_pic函數獲取圖片
    '''
    soup = BeautifulSoup(html, 'html.parser')
    pic_list = soup.find_all('li', class_='wp-item')
    for i in pic_list:
        a_tag = i.find('h3', class_='tit').find('a')
        link = a_tag.get('href')
        text = a_tag.get_text()
        get_pic(link, text)


def get_pic(link, text):
    '''
    獲取當前頁面的圖片,并保存
    '''
    html = download_page(link)  # 下載界面
    soup = BeautifulSoup(html, 'html.parser')
    pic_list = soup.find('div', id="picture").find_all('img')  # 找到界面所有圖片
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"}
    create_dir('pic/{}'.format(text))
    for i in pic_list:
        pic_link = i.get('src')  # 拿到圖片的具體 url
        r = requests.get(pic_link, headers=headers)  # 下載圖片,之后保存到文件
        with open('pic/{}/{}'.format(text, pic_link.split('/')[-1]), 'wb') as f:
            f.write(r.content)
            time.sleep(1)   # 休息一下,不要給網站太大壓力,避免被封


def create_dir(name):
    if not os.path.exists(name):
        os.makedirs(name)


def execute(url):
    page_html = download_page(url)
    get_pic_list(page_html)


def main():
    create_dir('pic')
    queue = [i for i in range(1, 72)]   # 構造 url 鏈接 頁碼。
    threads = []
    while len(queue) > 0:
        for thread in threads:
            if not thread.is_alive():
                threads.remove(thread)
        while len(threads) < 5 and len(queue) > 0:   # 最大線程數設置為 5
            cur_page = queue.pop(0)
            url = 'http://meizitu.com/a/more_{}.html'.format(cur_page)
            thread = threading.Thread(target=execute, args=(url,))
            thread.setDaemon(True)
            thread.start()
            print('{}正在下載{}頁'.format(threading.current_thread().name, cur_page))
            threads.append(thread)


if __name__ == '__main__':
    main()

            
          

更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 成人欧美s视频在线观看 | 亚洲一区视频在线 | 午夜视频在线观看网站 | 懂色中文一区二区三区在线视频 | 清纯唯美亚洲综合激情 | 亚洲国产综合久久精品 | 亚洲一区二区三区首页 | 91精品啪在线观看国产91九色 | 国产成人精品免高潮在线观看 | 99精品久久 | 99久久精品免费看国产免费 | 亚洲色四在线视频观看 | 欧美日韩一区在线 | 91热爆国产露脸 | 欧美8一10sex性hd | 91久久国产 | 一级女性大黄生活片免费 | 日韩国产午夜一区二区三区 | 久久成人免费网 | 成人在线97 | 91男女视频 | 亚洲欧美韩国日产综合在线 | 99久久精品免费看国产 | www.99xxxx.com| 欧美性猛交一区二区三区精品 | 丁香九月婷婷 | 二级毛片视频 | 国产电影精品 | 国产欧美日韩综合精品一区二区 | 日韩欧美一区二区三区不卡 | 91久操| 最新国产精品 | 久久国产精品毛片 | 亚洲视频在线看 | 成人国产精品免费 | 久久99国产一区二区三区 | 亚洲欧洲中文日韩 | 天天舔天天射天天操 | 久操综合 | av资源在线天堂 | 精品亚洲一区二区三区 |