gpt4 book ai didi

python - Scrapy - 每个页面都会被抓取,但 scrapy 会环绕并抓取前 x 数量的页面

转载 作者:行者123 更新时间:2023-12-02 04:57:03 26 4
gpt4 key购买 nike

class HomedepotcrawlSpider(CrawlSpider):

name = 'homeDepotCrawl'
#allowed_domains = ['homedepot.com']
start_urls =['https://www.homedepot.com/b/Appliances/ZLINE-Kitchen-and-Bath/N-5yc1vZbv1wZhsy?experienceName=default&Nao=0']

def parse(self, response):

for item in self.parseHomeDepot(response):
yield item

next_page_url = response.xpath('//link[@rel="next"]/@href').extract_first()
if next_page_url:
yield response.follow(url=next_page_url, callback=self.parse)



def parseHomeDepot(self, response):

items = response.css('.plp-pod')
for product in items:
item = HomedepotSpiderItem()

#get SKU
productSKU = product.css('.pod-plp__model::text').getall()

#get rid of all the stuff i dont need
productSKU = [x.strip(' ') for x in productSKU] #whiteSpace
productSKU = [x.strip('\n') for x in productSKU]
productSKU = [x.strip('\t') for x in productSKU]
productSKU = [x.strip(' Model# ') for x in productSKU] #gets rid of the model name
productSKU = [x.strip('\xa0') for x in productSKU] #gets rid of the model name



item['productSKU'] = productSKU

yield item

问题说明

这是我一直致力于抓取数据的程序的一部分。我省略了用于抓取其他字段的代码,因为我认为没有必要将其包含在这篇文章中。当我运行该程序并将数据导出到 Excel 时,我得到前 240 项(10 页)。这一直到我的电子表格的第 241 行(第一行被标签占据)。然后从第242行开始,再次重复前241行。然后再次在第 482 行和第 722 行。

抓取器输出前 240 个项目 3 次

编辑因此,我查看了抓取过程中的日志,结果发现每个页面都被抓取了。最后一页是:

https://www.homedepot.com/b/Appliances/ZLINE-Kitchen-and-Bath/N-5yc1vZbv1wZhsy?experienceName=default&Nao=696&Ns=None >

然后日志文件显示第一页再次被抓取,即:

https://www.homedepot.com/b/Appliances/ZLINE-Kitchen-and-Bath/N-5yc1vZbv1wZhsy?experienceName=default

我认为是因为.. enter image description here

我用来导出到 Excel 的终端命令是:

scrapy crawl homeDepotCrawl -t csv -o - > "(File Location)"

编辑:我使用此命令的原因是因为在导出时,Scrapy 将抓取的数据附加到文件中,因此这会删除目标文件并重新创建它。

我用来获取所有页面的代码是:

<a class="hd-pagination__link" title="Next" href="/b/Appliances/ZLINE-Kitchen-and-Bath/N-5yc1vZbv1wZhsy?experienceName=default&amp;Nao=24&amp;Ns=None" data-pagenumber="2"></a>

最初我认为是网站导致了这种意外行为,因此在 settings.py 上我更改了 ROBOTSXTXT_OBEY = 0 并添加了延迟,但这并没有改变任何内容。

所以我需要帮助:

-弄清楚为什么 CSV 输出只取前 240 个项目(10 页)并重复 3 次

-如何确保蜘蛛在抓取前 30 个页面后不会返回首页

最佳答案

我建议做这样的事情。主要区别是我从页面上存储的 json 中获取信息,并且通过识别 Nao 是产品偏移量来对自己进行分页。代码也短得多:

import requests,json,re
product_skus = set()
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36'}
base_url = 'https://www.homedepot.com/b/Appliances/ZLINE-Kitchen-and-Bath/N-5yc1vZbv1wZhsy?experienceName=default&Nao=%s'
for page_num in range(1,1000):
url = base_url % (page_num*24)
res = requests.get(url, headers=headers)
json_data = json.loads(re.search(r'digitalData\.content=(.+);', res.text).group(1))
prev_len = len(product_skus)
for product in json_data['product']:
product_skus.add(product['productInfo']['sku'])
if len(product_skus) == prev_len: break # this line is optional and can determine when you want to break

此外,Home Depot 页面似乎每 10 页重复一次(至少在您发送的内容中),这就是您看到 240 个限制的原因。这是我自己浏览的一个例子:

enter image description here

关于python - Scrapy - 每个页面都会被抓取,但 scrapy 会环绕并抓取前 x 数量的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60289197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com