鱼C论坛

 找回密码
 立即注册
查看: 1587|回复: 0

用scrapy框架爬取“斗图啦”网站表情包

[复制链接]
发表于 2020-10-11 11:11:00 | 显示全部楼层 |阅读模式
10鱼币
要求:用scrapy框架爬取“斗图啦”的表情包
2.使用ImagePipline管道

异步的源码就在这:
  1. import asyncio,aiohttp,os
  2. from parsel import Selector
  3. from time import time


  4. headers = {
  5.     'User-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
  6. }


  7. path = os.path.dirname(__file__)
  8. path = path + '/image'
  9. if not os.path.exists(path):
  10.     print('检测到没有容器')
  11.     os.mkdir(path)
  12.     print('生成完毕' + path)


  13. class Download(object):

  14.     def mk_url(self,startnum,endnum):
  15.         for num in range(startnum,endnum+1):
  16.             base_url = 'https://www.doutula.com/photo/list/?page={}'.format(num)
  17.             task_list = []
  18.             task_list.append(base_url)
  19.             yield task_list




  20.     async def fetch_html(self,session,url):
  21.         '''请求网页数据'''
  22.         async with session.get(url) as response:
  23.             return await response.text()

  24.     async def fetch_img(self,session,url):
  25.         '''请求图片数据'''
  26.         async with session.get(url) as data:
  27.             return await data.read()

  28.     async def parse_data(self,session,html):
  29.         '''处理数据'''
  30.         selector = Selector(html)
  31.         result_list = selector.xpath('//a[@class="col-xs-6 col-sm-3"]')

  32.         for result in result_list:
  33.             img_url = result.xpath('./img/@data-original').extract_first()
  34.             img_title = result.xpath('./img/@alt').extract_first()

  35.             all_title = img_title + '.' + img_url.split('.')[-1]

  36.             content = await self.fetch_img(session,img_url)

  37.             try:
  38.                 with open(path + "\" + all_title,mode='wb') as f:
  39.                     print("下载完成:",all_title)
  40.                     f.write(content)

  41.             except Exception as e:
  42.                 print(e)


  43.     async def start_save(self,url):
  44.         async with aiohttp.ClientSession(headers=headers) as session:
  45.             html = await self.fetch_html(session,url)
  46.             await self.parse_data(session = session, html = html)

  47.     async def download_pictures(self,startnum,endnum):
  48.         for page in range(startnum,endnum+1):
  49.             print("######正在下载第{}页数据######".format(page))
  50.             url_list = self.mk_url(startnum,endnum)
  51.             for url in url_list:
  52.                 base_url = url[0]
  53.                 await self.start_save(base_url)


  54. '''实例化'''
  55. if __name__ == '__main__':
  56.     print("任务启动中...")
  57.     download = Download()
  58.     loop = asyncio.get_event_loop()

  59.     tasks = [
  60.         asyncio.ensure_future(download.download_pictures(1,2000)),
  61.         asyncio.ensure_future(download.download_pictures(2001, 4000)),
  62.         
  63.     ]
  64.     start_time = time()
  65.     loop.run_until_complete(asyncio.gather(*tasks))
  66.     end_time = time()
  67.     run_time = end_time - start_time
  68.     print(run_time)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-28 03:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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