scrapy - Scrapy - 如何避免再次抓取同一页面
问题描述
我有非常简单的网站,有两种爬网路径。
主页https://amazn.com/list?page=1
列出所有产品
,每个产品都有一个url
指向详细信息页面的链接https://amzn.com/details/4ecf-wewe-adad-add3
class EcomSpider(scrapy.Spider):
name = 'ecom_spider'
allowed_domains = ['amazn.com']
start_urls = ['https://amazn.com/list?page=1']
def parse(self, response):
for item in response.xpath('.//div/'):
title = item.xpath('.//p[@class="title"]")]/text()').get()
price = item.xpath('.//span[@class="price"]/text()').get()
url = response.urljoin(item.xpath('.//h3//a/@href').get())
item_main = {
'title': title,
'price': price
'url': url,
}
yield scrapy.Request(url=url, callback=self.parse_details, meta={'item_main': item_main})
next_page = response.xpath('//a[@class="pagination"]').get()
if next_page is not None:
next_page_url = response.xpath('//a[@class="pagination"]/@href').get()
print("next page url", next_page_url)
yield scrapy.Request(url=next_page_url, callback=self.parse)
def parse_details(self, response):
item_main = response.meta['item_main']
item_main['description'] = response.xpath('//div[@class="description"]/text()').get()
year = response.xpath('//div[@class="year"]/text()').get()
yield item_main
并将结果保存到 CSV 文件
这里的问题是主页/list?page=1
每天都会更新新产品,所以第二天当我运行爬虫时,它会找到相同的产品,/list?page=2
而不是/list?page=1
将重复的产品保存到 CSV 文件中。
我听说JOBDIR
将 URL 存储为 sha1 以及它在我的场景中是如何工作的。scrapy框架中还有其他可用的设置吗?
我对避免爬取以前爬过的路线特别感兴趣,/details/
因为/details/
包含大约100K pages
并且数据永远不会改变并且/list
大约为 1K。
解决方案
推荐阅读
- vba - 在 PowerPoint 中使用 VBA 更新 msoLinkedOLEObject 时如何绕过 Excel 弹出窗口
- javascript - 使用文本字段同时过滤 react-table 和 react-leaflet 标记(在表中显示过滤器数据并在地图中显示标记)
- bezier - 将 3 次立方 Nurbs 曲线转换为 Catmull-Rom?
- python - 确定 fileinput.input 是否有任何输入?
- go - Go 模板不会显示结构切片
- php - array_search 找不到以 `@` 开头的字符串
- git - 创建本地目录并推送到 GitHub 的简洁命令行步骤
- java - JMOD 架构会影响 Javac 生成的类文件吗
- java - 将图像从内部存储上传到服务器 [错误]
- javascript - 从用户输入发送 JSON 对象中的日期