python - Scrapy - 达到最大重定向时生成 URL [301]

标签 python html networking scrapy

代码

# -*- coding: utf-8 -*-
import scrapy
import pandas as pd
from ..items import Homedepotv2Item
from scrapy.http import HtmlResponse


class HomedepotspiderSpider(scrapy.Spider):
    name = 'homeDepotSpider'
    #allowed_domains = ['homedepot.com']

    start_urls = ['https://www.homedepot.com/p/305031636', 'https://www.homedepot.com/p/311456581']
    handle_httpstatus_list = [301]

def parseHomeDepot(self, response):

    #get top level item
    items = response.css('.pip-container-fluid')
    for product in items:
        item = Homedepotv2Item()

        productSKU = product.css('.modelNo::text').getall() #getSKU

        productURL = response.request.url #Get URL


        item['productSKU'] = productSKU
        item['productURL'] = productURL

        yield item

终端消息

没有handle_httpstatus_list = [301]

2020-03-12 12:24:58 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (301) to <GET https://www.homedepot.com/p/ZLINE-Kitchen-and-Bath-ZLINE-30-in-Wall-Mount-Range-Hood-in-DuraSnow-%C3%91-Stainless-Steel-8687S-30-8687S-30/305031636> from <GET https://www.homedepot.com/p/ZLINE-Kitchen-and-Bath-ZLINE-30-in-Wall-Mount-Range-Hood-in-DuraSnow-%C3%91-Stainless-Steel-8687S-30-8687S-30/305031636>
2020-03-12 12:24:58 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.homedepot.com/p/ZLINE-Kitchen-and-Bath-ZLINE-30-in-Wooden-Wall-Mount-Range-Hood-in-Walnut-Includes-Remote-Motor-KBRR-RS-30/311456581>
{'productName': ['ZLINE 30 in. Wooden Wall Mount Range Hood in Walnut - '
                 'Includes  Remote Motor'],
 'productOMS': '311456581',
 'productPrice': ['                                    979.95'],
 'productSKU': ['KBRR-RS-30'],
 'productURL': 'https://www.homedepot.com/p/ZLINE-Kitchen-and-Bath-ZLINE-30-in-Wooden-Wall-Mount-Range-Hood-in-Walnut-Includes-Remote-Motor-KBRR-RS-30/311456581'}

2020-03-12 12:25:01 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (301) to <GET https://www.homedepot.com/p/ZLINE-Kitchen-and-Bath-ZLINE-30-in-Wall-Mount-Range-Hood-in-DuraSnow-%C3%91-Stainless-Steel-8687S-30-8687S-30/305031636> from <GET https://www.homedepot.com/p/ZLINE-Kitchen-and-Bath-ZLINE-30-in-Wall-Mount-Range-Hood-in-DuraSnow-%C3%91-Stainless-Steel-8687S-30-8687S-30/305031636>
2020-03-12 12:25:01 [scrapy.downloadermiddlewares.redirect] DEBUG: Discarding <GET https://www.homedepot.com/p/ZLINE-Kitchen-and-Bath-ZLINE-30-in-Wall-Mount-Range-Hood-in-DuraSnow-%C3%91-Stainless-Steel-8687S-30-8687S-30/305031636>: max redirections reached
2020-03-12 12:25:01 [scrapy.core.engine] INFO: Closing spider (finished)
2020-03-12 12:25:01 [scrapy.extensions.feedexport] INFO: Stored csv feed (1 items) in: stdout:

使用handle_httpstatus_list = [301]

2020-03-12 12:27:30 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-03-12 12:27:31 [scrapy.core.engine] DEBUG: Crawled (301) <GET https://www.homedepot.com/p/305031636> (referer: None)
2020-03-12 12:27:31 [scrapy.core.engine] DEBUG: Crawled (301) <GET https://www.homedepot.com/p/311456581> (referer: None)
2020-03-12 12:27:31 [scrapy.core.engine] INFO: Closing spider (finished)

这就是我用来导出到Excel的scrapycrapy homeDepotSpider -t csv -o ->“pathname”

问题

所以我最初遇到的问题是“https://www.homedepot.com/p/305031636 ”被我的蜘蛛忽略,因为该链接会丢弃错误代码 301(重定向过多)。研究问题后,我发现 handle_httpstatus_list = [301] 应该已经解决了这个问题。然而,当我使用这段代码时,由于工作链接(“https://www.homedepot.com/p/311456581”)重定向到不同的页面,它也会被忽略。

本质上我想要做的是从所有没有

的 URL 中抓取数据
ERR_TOO_MANY_REDIRECTS  

但从确实具有该错误代码的链接中获取 URL,然后将该数据导出到 Excel。

编辑:提出问题的更好方法:由于我正在使用的所有 URL 都经过重定向,我如何处理无法重定向并获取这些 URL 的页面?

此外,这不是我的整个程序,我只包含了我认为该程序必需的部分。

最佳答案

您可以手动处理 301 错误代码,如下所示:

class HomedepotspiderSpider(scrapy.Spider):
    name = 'my_spider'
    retry_max_count = 3

    start_urls = ['https://www.homedepot.com/p/305031636', 'https://www.homedepot.com/p/311456581']
    handle_httpstatus_list = [301]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.retries = {} # here are stored URL retries counts

    def parse(self, response):
        if response.status == 301:
            retries = self.retries.setdefault(response.url, 0)
            if retries < self.retry_max_count:
                self.retries[response.url] += 1
                yield response.request.replace(dont_filter=True)
            else:
                # ...
                # DO SOMETHING TO TRACK ERR_TOO_MANY_REDIRECTS AND SAVE response.url
                # ...

            return

请注意,302 代码也可用于重定向。

关于python - Scrapy - 达到最大重定向时生成 URL [301],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60624455/

相关文章:

python - 保持 python 调度程序脚本在 Windows 上运行

javascript - 如何将 iframe 中的 div 滚动到另一个 iframe 中的 anchor

c# - DHCP 地址 255.255.255.255

javascript - 为什么IE11、Edge只能存储2个cookie?

html - Bootstrap 响应式图像网格

android - 没有到主机的路由

networking - 用于网络故障的测试工具软件

python - 创建 tf-idf 值矩阵

python - 在 python 中从给定模式(通配符)生成所有二进制字符串

python - 使用 PyQt 在 QTableView 中指定索引