Scrapy - 如何停止元刷新重定向?

gun*_*tan 4 python redirect http scrapy web-scraping

是我正在爬行的网站。一开始我没有问题,但是后来我遇到了这个错误。

[scrapy] DEBUG: Redirecting (meta refresh) to <GET https://www.propertyguru.com.my/distil_r_captcha.html?requestId=9f8ba25c-3673-40d3-bfe2-6e01460be915&httpReferrer=%2Fproperty-for-rent%2F1> from <GET https://www.propertyguru.com.my/property-for-rent/1>
Run Code Online (Sandbox Code Playgroud)

网站知道我是机器人,并将我重定向到带有验证码的页面。我认为handle_httpstatus_listordont_redirect不起作用,因为重定向不是通过 http 状态代码完成的。这是我的爬虫代码。有什么办法可以阻止这种重定向吗?

class MySpider(CrawlSpider):

    name = 'myspider'

    start_urls = [
        'https://www.propertyguru.com.my/property-for-rent/1',
    ]

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
    }
    meta = {
        'dont_redirect': True
    }


    def parse(self, response):    
        items = response.css('div.header-container h3.ellipsis a.nav-link::attr(href)').getall()

        if items:
            for item in items:
                if item.startswith('/property-listing/'):
                    yield scrapy.Request(
                        url='https://www.propertyguru.com.my{}'.format(item),
                        method='GET',
                        headers=self.headers,
                        meta=self.meta,
                        callback=self.parse_items
                    )

    def parse_items(self, response):
        from scrapy.shell import inspect_response
        inspect_response(response, self)
Run Code Online (Sandbox Code Playgroud)

更新:我尝试了这些设置,但它们也不起作用。

custom_settings = {
    'DOWNLOAD_DELAY': 5,
    'DOWNLOAD_TIMEOUT': 360,
    'CONCURRENT_REQUESTS_PER_DOMAIN': 1,
    'CONCURRENT_ITEMS': 1,
    'REDIRECT_MAX_METAREFRESH_DELAY': 200,
    'REDIRECT_MAX_TIMES': 40,
}
Run Code Online (Sandbox Code Playgroud)

The*_*Guy 5

本网站受 Distil Networks 保护。他们使用 JavaScript 来确定您是机器人。他们是允许某些请求通过还是根本不通过?您也许可以使用 Selenium 取得一些成功,但根据我的经验,它们最终会流行起来。该解决方案涉及根据屏幕尺寸和您能想到的所有其他内容随机化整个浏览器指纹。如果其他人有更多信息,我将有兴趣了解。我不确定此类内容的 SoF ToS。

如果你加载像 charles proxy 之类的代理,这样你就可以看到发生的一切,你可以查看它们在你身上运行的所有 JS。

如果他们允许 0 个请求通过,我建议使用 Selenium 来看看你的运气。

如果他们让一些人通过并重新引导其他人,我的经验是,随着时间的推移,他们最终会重新引导所有的人。如果他们让一些通过,我会做的就是设置 http_retry_codes = []

为了进一步扩展这一点,我将链接到这篇关于使用 Selenium 覆盖导航器对象的文章,Selenium 包含了您的大部分浏览器指纹。它必须在 JS 中完成并在每个页面加载时完成。我无法证明它对 Distil 的有效性。看这个答案

感谢其他答案完成我的回答,直接回答您的问题。

#settings.py

HTTP_RETRY_CODES = [404, 303, 304, ???]
RETRY_TIMES = 20

DOWNLOADER_MIDDLEWARES = {
    'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware': None,
}

Run Code Online (Sandbox Code Playgroud)

在特定请求的蜘蛛元属性中:

meta={'dont_redirect': True}
Run Code Online (Sandbox Code Playgroud)

另外值得注意的是,您可以在 process_response 方法下的中间件中捕获 302 并让它丢弃另一个请求。如果您有良好的 UA 列表和 IP 源,那么与 HTTP RETRY CODES 结合使用是一种很好的暴力破解方法。

我建议您尝试https://scrapinghub.com/crawlera。他们最近提高了价格,但他们提供了良好的 IP 并检测禁令。如果您需要获取某些信息,这确实是值得的。他们的网络非常智能,不像大多数便宜得多的 IP 轮换网络。他们正在进行试用,因此您可以验证它是否有效,并且它是由 scrapy 开发人员制作的,因此请按照文档轻松安装

pip install scrapy_crawlera
Run Code Online (Sandbox Code Playgroud)

然后你可以重试所有这些,直到你的旋转器给你一个好的IP,我怀疑你会发现在很短的时间内它们都会被禁止。