当前位置: 首页 > news >正文

Python爬虫——scrapy_crawlspider读书网

创建crawlspider爬虫文件:

scrapy genspider -t crawl 爬虫文件名 爬取的域名scrapy genspider -t crawl read https://www.dushu.com/book/1206.html

LinkExtractor 链接提取器通过它,Spider可以知道从爬取的页面中提取出哪些链接,提取出的链接会自动生成Request请求对象

class ReadSpider(CrawlSpider):name = "read"allowed_domains = ["www.dushu.com"]start_urls = ["https://www.dushu.com/book/1206_1.html"]# LinkExtractor 链接提取器通过它,Spider可以知道从爬取的页面中提取出哪些链接。提取出的链接会自动生成Request请求对象rules = (Rule(LinkExtractor(allow=r"/book/1206_\d+\.html"), callback="parse_item", follow=False),)def parse_item(self, response):name_list = response.xpath('//div[@class="book-info"]//img/@alt')src_list = response.xpath('//div[@class="book-info"]//img/@data-original')for i in range(len(name_list)):name = name_list[i].extract()src = src_list[i].extract()book = ScarpyReadbook41Item(name=name, src=src)yield book

开启管道、
写入文件

class ScarpyReadbook41Pipeline:def open_spider(self, spider):self.fp = open('books.json', 'w', encoding='utf-8')def process_item(self, item, spider):self.fp.write(str(item))return itemdef close_spider(self, spider):self.fp.close()

运行之后发现没有第一页数据
需要在start_urls里加上_1,不然不会读取第一页数据

start_urls = ["https://www.dushu.com/book/1206_1.html"]
http://www.lryc.cn/news/131423.html

相关文章:

  • Spring源码编译-for mac
  • 视频汇聚平台EasyCVR安防监控视频汇聚平台的FLV视频流在VLC中无法播放的问题解决方案
  • 中间件:RocketMQ安装部署
  • leetcode-动态规划-42-接雨水
  • [静态时序分析简明教程(十一)]浅议tcl语言
  • 大数据-玩转数据-Flink 网站UV统计
  • 3分钟了解下cwnd和TCP拥塞控制算法
  • 设计模式之状态模式(State)的C++实现
  • 无涯教程-TensorFlow - Keras
  • 使用SSH隧道将Ubuntu云服务器Jupyter Notebook端口映射到本地
  • Keepalived+LVS部署高可用集群
  • 2023河南萌新联赛第(五)场:郑州轻工业大学
  • 在Orangepi5开发板3588s使用opencv获取摄像头画面
  • 音视频 ffmpeg命令分类查询
  • VSCode无法从Extensions下载工具时,把工具下载到本地并添加到VSCode编辑器
  • WebStrom 前端项目Debug
  • 【ARM Linux 系统稳定性分析入门及渐进12 -- GDB内存查看命令 “x“(examine)】
  • kube-prometheus 系列1 项目介绍
  • 深度学习在组织病理学图像分析中的应用: Python实现和代码解析
  • kotlin的列表
  • PCL 三维点云边界提取(C++详细过程版)
  • ../../ 目录遍历
  • clickhouse集群部署
  • centos8 使用phpstudy安装tomcat部署web项目
  • 爬虫百度返回“百度安全验证”终极解决方案
  • visual studio 2022配置
  • B-树和B+树的区别
  • c注册cpp回调函数
  • 批量将excel中字段为“八百”替换成“九百”
  • 关于docker-compose up -d在文件下无法运行的原因以及解决方法