scrapy爬虫翻页爬取诗词胖排行榜案例

2022-10-1820:43:32后端程序开发Comments754 views字数 405阅读模式

scrapy框架实现自动翻页爬取数据,爬取诗词胖排行榜(
https://www.shicimingju.com/paiming)中的标题。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

1.新建文件夹文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

scrapy starproject 文件名(wallpaper)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

scrapy爬虫翻页爬取诗词胖排行榜案例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

2.新建爬虫文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

scrapy genspider 文件名(landscape)www.xxx.com文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

scrapy爬虫翻页爬取诗词胖排行榜案例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

3.发送请求并解析数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

上两节课有详细的讲解,所以这部分不再赘述。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

scrapy爬虫翻页爬取诗词胖排行榜案例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

4.建立翻页链接文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

上面我们放的链接为第一页的地址,根据翻页网页的地址我们可以发现,“p”对应的值为变量,因此我们拼接网址:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

scrapy爬虫翻页爬取诗词胖排行榜案例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

scrapy爬虫翻页爬取诗词胖排行榜案例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

5.对翻页链接发送请求文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

基本格式为:yield scrapy.Request(url地址,callback=self.parse),当遇到第二页的时候,会根据上面的拼接地址填入,并解析数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

scrapy爬虫翻页爬取诗词胖排行榜案例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

6.总结文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

要实现翻页自动发送请求,需要先建立好第一页的请求并解析,然后写入翻页的网址之后,通过调用yield关键字实现翻页自动发送请求文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28358.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/28358.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定