Chrome 零代码爬虫神器 Web Scraper:翻页/分页 场景的爬取

作者: 王炳明 分类: 软件大杂烩 发布时间: 2021-11-07 17:12 热度:2,637

分页器可以分为两种:

  • 一种是,点 下一页 就会重新加载一个页面
  • 一种是:点 下一页 只是当前页面的部分内容重新渲染

在早期的 web-scraper 版本中,这两种的爬取方法有所不同。

  • 对于需要重新加载页面的,需要 Link 选择器
  • 对于不需要重新加载页面的,可以使用 Element Click 选择器

对于某些网站的确是够用了,但却有很大的局限性。

经过我的试验,第一种使用 Link 选择器的原理就是取出 下一页 的 a 标签的超链接,然后去访问,但并不是所有网站的下一页都是通过 a 标签实现。

像下面这样用 js 监听事件然后跳转的,就无法使用 Link 选择器 。

Chrome 零代码爬虫神器 Web Scraper:翻页/分页 场景的爬取

而在新版的 web scraper ,对导航分页器提供了特别的支持,加了一个 Pagination 的选择器,可以完全适用两种场景,下面我会分别演示。

不重载页面的分页器爬取

CSDN 的评论区分页属于这种不重新加载页面。

Chrome 零代码爬虫神器 Web Scraper:翻页/分页 场景的爬取

sitemap 的配置如下,你可以直接导入使用

{"_id":"blog_comment","startUrl":["https://blog.csdn.net/weixin_36338224/article/details/111936614"],"selectors":[{"id":"next_page","parentSelectors":["_root","next_page"],"paginationType":"auto","selector":"li.js-page-next","type":"SelectorPagination"},{"id":"comment","parentSelectors":["_root","next_page"],"type":"SelectorElement","selector":"ul:nth-of-type(n+2) > li.comment-line-box span.new-comment","multiple":true,"delay":0},{"id":"content","parentSelectors":["comment"],"type":"SelectorText","selector":"_parent_","multiple":false,"delay":0,"regex":""}]}

查看爬取路径拓扑如下

Chrome 零代码爬虫神器 Web Scraper:翻页/分页 场景的爬取

要重载页面的分页器爬取

CSDN 的博客文章列表属于这种需要重新加载页面。

sitemap 的配置如下,你可以直接导入使用

{"_id":"mycsdn","startUrl":["https://blog.csdn.net/weixin_36338224/article/list/1"],"selectors":[{"id":"next_page","parentSelectors":["_root","next_page"],"paginationType":"auto","selector":"li.js-page-next","type":"SelectorPagination"},{"id":"article","parentSelectors":["_root","next_page"],"type":"SelectorElement","selector":"div.article-item-box","multiple":true,"delay":0},{"id":"title","parentSelectors":["article"],"type":"SelectorText","selector":"h4 a","multiple":false,"delay":0,"regex":""},{"id":"views","parentSelectors":["article"],"type":"SelectorText","selector":"span:nth-of-type(2)","multiple":false,"delay":0,"regex":"[0-9]+"},{"id":"comments","parentSelectors":["article"],"type":"SelectorText","selector":"span:nth-of-type(3)","multiple":false,"delay":0,"regex":""},{"id":"publish_time","parentSelectors":["article"],"type":"SelectorText","selector":"span.date","multiple":false,"delay":0,"regex":""}]}

查看爬取路径拓扑如下

Chrome 零代码爬虫神器 Web Scraper:翻页/分页 场景的爬取

文章有帮助,请作者喝杯咖啡?

发表评论