查看: 646|回复: 0

[电脑应用] [Python] 【多线程】美女壁纸爬虫,请注意身体!

[复制链接]
累计签到:390 天
连续签到:2 天

53

主题

134

回帖

6823

积分

域主

名望
236
星币
4323
星辰
7
好评
35

欢乐天使奖灌水天才奖鼎力支持奖幸运猪星辰勋章优秀会员奖明星会员奖魅力会员奖国庆节勋章在线大神

QQ
发表于 2022-8-6 14:36:20 | 显示全部楼层 |阅读模式

注册登录后全站资源免费查看下载

您需要 登录 才可以下载或查看,没有账号?立即注册

×
【多线程爬虫成品EXE下载地址】链接: https://pan.baidu.com/s/1QymE_5q7e3bkHax7nIvkGw?pwd=tspe 提取码: tspe
【多线程爬虫成品EXE下载地址】链接:https://share.weiyun.com/86w0CFW7 密码:6jt488

这个网站,有2W多张美图~~,别给人网站搞坏了,我就给了5个线程,自己会动手的,自己改吧,成品就5个线程爬~
【异步爬虫成品下载地址】
https://www.paijishu.com/forum.php?mod=viewthread&tid=8752

多线程爬虫代码


  1. from collections import namedtuple
  2. from concurrent.futures import ThreadPoolExecutor
  3. from typing import Dict, List
  4. import re
  5. import requests
  6. import os
  7. from datetime import datetime
  8. import keyboard
  9. from fake_useragent import UserAgent
  10. from lxml import etree
  11. from rich.console import Console

  12. console = Console()
  13. headers = {'User-Agent':UserAgent().random}
  14. DATA = namedtuple('DATA',['year','month','day','title','href'])
  15. url = 'https://www.vmgirls.com/archives.html'
  16. img_list = ['jpg','png','gif','jpeg']

  17. def start_requests():
  18.     '''
  19.     获取下载链接
  20.     '''
  21.     res = requests.get(url,headers=headers)
  22.     et = etree.HTML(res.text)
  23.     # 获取全部年份
  24.     y = et.xpath('//div[@id="archives"]/h4/text()')
  25.     for year in range(1,len(y)+1):
  26.         # 每个月
  27.         m = et.xpath(f'//div[@id="archives"]//ul[{year}]/li/span/text()')
  28.         for month in range(1,len(m)+1):
  29.             # 每天
  30.             d = et.xpath(f'//div[@id="archives"]//ul[{year}]/li[{month}]/ul/li')
  31.             for day in range(1,len(d)+1):
  32.                 # 每天的网址
  33.                 _day = et.xpath(f'//div[@id="archives"]//ul[{year}]/li[{month}]/ul/li[{day}]/text()')[0]
  34.                 _href = et.xpath(f'//div[@id="archives"]//ul[{year}]/li[{month}]/ul/li[{day}]/a/@href')[0]
  35.                 _title = et.xpath(f'//div[@id="archives"]//ul[{year}]/li[{month}]/ul/li[{day}]/a/text()')[0]
  36.                 yield DATA(y[year-1],m[month-1],_day,_title,_href)


  37. def get_data(yield_func):
  38.     '''
  39.     转换数据
  40.     '''
  41.     yield from yield_func


  42. def save_img(url,path,title):
  43.     '''
  44.     保存图片
  45.     '''
  46.     imgcs = requests.get(url,headers=headers)
  47.     et = etree.HTML(imgcs.text)
  48.     IMG = et.xpath('//div[@class="nc-light-gallery"]//@href')
  49.     for i in range(0,len(IMG)-1):
  50.         path = mkdir_path(path)
  51.         if IMG[i].split('.')[-1] in img_list:
  52.             res = requests.get(IMG[i],headers=headers)
  53.             with open(f'{path}/{title}_{i}.jpg','wb') as f:
  54.                 f.write(res.content)
  55.                 nowdate = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  56.                 console.print(f'[yellow]创建时间:{nowdate}\n[yellow]保存路径:{path}\n[yellow]文件:{title}_{i}.jpg 保存成功!\n[green]提示:按esc退出')
  57.                 console.print('[blue]-'*70)
  58.         if keyboard.read_key() == 'esc':
  59.             raise KeyboardInterrupt


  60. def mkdir_path(path):
  61.     '''
  62.     创建路径
  63.     '''
  64.     path = re.sub(r'[\s]','',path)
  65.     if not os.path.exists(path):
  66.         os.makedirs(path)
  67.     return path

  68. def main():
  69.     '''
  70.     多线程主函数
  71.     '''
  72.     with ThreadPoolExecutor(max_workers=5) as executor:
  73.         try:
  74.             for data in get_data(start_requests()):
  75.                 path = os.path.join(os.getcwd(),'美女壁纸',data.year,data.month,data.day[:-2])
  76.                 img_name = data.title
  77.                 url = data.href
  78.                 executor.submit(save_img,url,path,img_name)
  79.         except Exception as e:
  80.             print(e)
  81.             console.print('[red]程序即将退出!')
  82.             os._exit(0)            

  83. if __name__ == '__main__':
  84.     main()
复制代码
效果图
多线程.png
对哦.png
多.png


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|偏爱技术社区-偏爱技术吧-源码-科学刀-我爱辅助-娱乐网--教开服-游戏源码

偏爱技术社区-偏爱技术吧-源码-科学刀-我爱辅助-娱乐网-游戏源码

Powered by Discuz! X3.5

GMT+8, 2025-9-12 20:30 , Processed in 0.083259 second(s), 38 queries .

快速回复 返回顶部 返回列表