鱼C论坛

 找回密码
 立即注册
查看: 1891|回复: 2

[作品展示] Scrapy爬取漫画

[复制链接]
发表于 2020-7-8 15:59:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
本帖最后由 lhgzbxhz 于 2020-7-8 16:01 编辑

废话不多说,直接上代码
1、
  1. cd F:\编程\Python\Scrapy
  2. Scrapy startproject mkz
复制代码


2、
  1. # in spiders\chapters.py
  2. import scrapy


  3. class ChapterSpider(scrapy.Spider):
  4.     name = "chapter"
  5.     start_urls = ["https://www.mkzhan.com/211692/"]

  6.     def parse(self, response: scrapy.http.Response):
  7.         for chapter in response.css("a.j-chapter-link"):
  8.                 # 在<a>标签内有另外的标签
  9.                 # 所以要用//text()获取所有文本
  10.             title = chapter.xpath("..//text()").extract()
  11.             if title is None:
  12.                 self.log("None!")
  13.                 continue
  14.             # 获取到的文本中有"\n  "以及"     "一类
  15.             # 所以要先strip(),再 if t != ""
  16.             for i in range(len(title)):
  17.                 title[i] = title[i].strip()
  18.             yield {
  19.                 "title": [t for t in title if t != ""][0],
  20.                 "url": response.urljoin(chapter.css("::attr(data-hreflink)").extract_first()),
  21.             }
复制代码


3、
  1. Scrapy crawl chapter -o ch.json
复制代码

爬取到的ch.json节选如下:
  1. [
  2. {“title”: “\u7b2c553\u8bdd \u6838\u5fc3\u533a2”, “url”: “https://www.mkzhan.com/211692/903557.html”},
  3. {“title”: “\u7b2c552\u8bdd \u6838\u5fc3\u533a1”, “url”: “https://www.mkzhan.com/211692/903553.html”},
  4. {“title”: “\u7b2c551\u8bdd \u9053\u6b492”, “url”: “https://www.mkzhan.com/211692/902374.html”},
  5. {“title”: “\u7b2c550\u8bdd \u9053\u6b491”, “url”: “https://www.mkzhan.com/211692/902375.html”},
  6. {“title”: “\u7b2c549\u8bdd \u82cf\u91922”, “url”: “https://www.mkzhan.com/211692/901306.html”},

  7. ]
复制代码


4、
  1. # in spiders\images.py
  2. import scrapy
  3. import requests
  4. import json
  5. import os


  6. class ImageSpider(scrapy.Spider):
  7.     name = "images"

  8.     def start_requests(self):
  9.         with open("ch.json", 'r') as f:
  10.             chapters_list = json.load(f)
  11.         for chapter in chapters_list:
  12.                 # 此处使用了requests对象的meta属性,具体的大家可以自行百度
  13.             yield scrapy.Request(chapter["url"], callback=self.parse, meta={"title": chapter["title"]})

  14.     def img_parse(self, response):
  15.         with open(response.meta["path"], 'wb') as f:
  16.             f.write(response.body)

  17.     def parse(self, response):
  18.         title = response.meta["title"]
  19.         img_tags = response.xpath('//div[@class="rd-article__pic hide"]')
  20.         page_ids = []
  21.         image_urls = {}
  22.         for tag in img_tags:
  23.             # 按照data-page_id排序
  24.             page_id = int(tag.xpath('./@data-page_id').extract_first())
  25.             page_ids.append(page_id)
  26.             image_urls[page_id] = response.urljoin(tag.xpath('./img/@data-src').extract_first())
  27.         page_ids.sort()
  28.         # 创建文件夹
  29.         try:
  30.             os.mkdir(".\\images\" + title)
  31.         except FileExistsError:
  32.             pass  # 如果文件夹已存在

  33.         for page_id in page_ids:
  34.             url = image_urls[page_id]
  35.             # 文件格式:".\images\%title%\%page_id%.jpg"
  36.             # 为了使爬取到的图片有序,必须这么干
  37.             yield scrapy.Request(url, callback=self.img_parse, meta={"path": ".\\images\" + title + '\\' + str(page_id) + ".jpg"})

复制代码


5、
  1. Scrapy crawl images
复制代码

爬取效果:

                               
登录/注册后可看大图


                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-7-17 17:33:54 | 显示全部楼层
怎么一直没人呀~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-17 22:45:49 From FishC Mobile | 显示全部楼层
不隐藏,沉得快
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-23 20:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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