【Python爬蟲】尺度太大了!爬一個專門看小姐姐的網站,寫一段緊張刺激的代碼(附源碼)


前言

今天我們通過Python爬取小姐姐圖片網站上的美圖,零基礎學會通用爬蟲,當然我們還可以實現多線程爬蟲,加快爬蟲速度

環境介紹

  • python 3.6
  • pycharm
  • requests >>> pip install requests
  • re
  • time
  • concurrent.futures

 

爬蟲最基本思路

爬取單個相冊內容:

  1. 找到目標 https://https://www.kanxiaojiejie.com/img/6509
  2. 發送請求 (人為操作: 訪問網站)
  3. 獲取數據 (HTML代碼 就是服務器返回的數據)
  4. 數據提取 (篩選里面的內容)
    HTML網頁代碼
  5. 保存數據 (把圖片下載下來)

目標網站

簡單的通用爬蟲代碼

import requests
import parsel
import re
import os

page_html = requests.get('https://www.kanxiaojiejie.com/page/1').text
pages = parsel.Selector(page_html).css('.last::attr(href)').get().split('/')[-1]
for page in range(1, int(pages) + 1):
    print(f'==================正在爬取第{page}頁==================')
    response = requests.get(f'https://www.kanxiaojiejie.com/page/{page}')
    data_html = response.text
    # 提取詳情頁
    zip_data = re.findall('<a href="(.*?)" target="_blank"rel="bookmark">(.*?)</a>', data_html)
    for url, title in zip_data:
        print(f'----------------正在爬取{title}----------------')
        if not os.path.exists('img/' + title):
            os.mkdir('img/' + title)
        resp = requests.get(url)
        url_data = resp.text
        selector = parsel.Selector(url_data)
        img_list = selector.css('p>img::attr(src)').getall()

        for img in img_list:
            img_data = requests.get(img).content
            img_name = img.split('/')[-1]
            with open(f"img/{title}/{img_name}", mode='wb') as f:
                f.write(img_data)
            print(img_name, '爬取成功!!!')
        print(title,'爬取成功!!!')

 

升級 多線程版本

把每一塊都封裝一個函數, 每個函數都有它特定的功能

先導入模塊

import requests # 第三方模塊 pip install requests
import re # 正則表達式模塊 內置模塊
import time
import concurrent.futures
import os
import parsel

 

發送請求

def get_response(html_url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
    }
    # 為什么這里要 requests.get()  post() 請求會更安全...
    response = requests.get(url=html_url, headers=headers)
    return response

 

保存數據

def save(title, img_url):
    img_data = requests.get(img_url).content
    img_name = img_url.split('/')[-1]
    with open("img\\" + title + '\\' + img_name, mode='wb') as f:
        f.write(img_data)

 

解析數據 獲取圖片url地址以及標題

def parse_1(data_html):
    zip_data = re.findall('<a href="(.*?)" target="_blank"rel="bookmark">(.*?)</a>', data_html, re.S)
    return zip_data

 

解析數據 獲取圖片url地址以及標題

def parse_2(html_data):
    selector = parsel.Selector(html_data)
    img_list = selector.css('p>img::attr(src)').getall()
    return img_list

 

創建文件夾

def mkdir_img(title):
    if not os.path.exists('img\\' + title):
        os.mkdir('img\\' + title)

 

主函數

def main(html_url):
    html_data = requests.get(html_url).text
    zip_data = parse_1(html_data)
    for url, title in zip_data:
        mkdir_img(title)
        html_data_2 = get_response(url).text
        img_list = parse_2(html_data_2)
        for img in img_list:
            save(title, img)
        print(title, '爬取成功!!!')

 

程序的入口

if __name__ == '__main__':
    time_1 = time.time()
    exe = concurrent.futures.ThreadPoolExecutor(max_workers=10)
    for page in range(1, 11):
        url = f'https://www.kanxiaojiejie.com/page/{page}'
        exe.submit(main, url)
    exe.shutdown()
    time_2 = time.time()
    use_time = int(time_2) - int(time_1)
    print(f'總計耗時:{use_time}秒')

 

總耗時:80秒


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM