Scrapy翻页爬取示例——列表页、详情页
引言: 本人最近在帮助同事们爬取一批英—泰双语数据,顺带复习了一下scrapy爬虫相关的知识。下面以简单的小项目为例,一起来开始吧!
示例一:爬取列表页
本文以这个网站为例:https://engoo.co.th/app/words/list/en/a
网站首页如图:
向下翻会看到翻页的小图标:
假如我们要获取1-17页该页面上所有的字符串,如下所示:
应该如何做?
先给出代码(仅逻辑实现部分):
def parse(self, response):
divs = response.xpath("//div[@class='css-rv942s']")
for div in divs:
item = ThaiItem()
item['en'] = div.xpath("./span[@class='css-1sylyko']/a/text()").extract_first()
item['thai'] = div.xpath("./span[@class='css-epvm6']/span/div[@class='css-jiq801']/text()")