starturl 上的 scrapy 503 服务不可用

scrapy 503 Service Unavailable on starturl

我修改了这个蜘蛛,但它给出了这个错误

Gave up retrying <GET https://lib.maplelegends.com/robots.txt> (failed 3 times): 503 Service Unavailable 
2019-01-06 23:43:56 [scrapy.core.engine] DEBUG: Crawled (503) <GET https://lib.maplelegends.com/robots.txt> (referer: None)
2019-01-06 23:43:56 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET https://lib.maplelegends.com/?p=etc&id=4004003> (failed 1 times): 503 Service Unavailable
2019-01-06 23:43:56 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET https://lib.maplelegends.com/?p=etc&id=4004003> (failed 2 times): 503 Service Unavailable
2019-01-06 23:43:56 [scrapy.downloadermiddlewares.retry] DEBUG: Gave up retrying <GET https://lib.maplelegends.com/?p=etc&id=4004003> (failed 3 times): 503 Service Unavailable
2019-01-06 23:43:56 [scrapy.core.engine] DEBUG: Crawled (503) <GET https://lib.maplelegends.com/?p=etc&id=4004003> (referer: None)
2019-01-06 23:43:56 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <503 https://lib.maplelegends.com/?p=etc&id=4004003>: HTTP status code is not handled or not allowed

爬虫代码:

#!/usr/bin/env python3

import scrapy
import time

start_url = 'https://lib.maplelegends.com/?p=etc&id=4004003'


class MySpider(scrapy.Spider):
    name = 'MySpider'

    start_urls = [start_url]

    def parse(self, response):
        # print('url:', response.url)

        products = response.xpath('.//div[@class="table-responsive"]/table/tbody')

        for product in products:
            item = {
                #'name': product.xpath('./tr/td/b[1]/a/text()').extract(),
                'link': product.xpath('./tr/td/b[1]/a/@href').extract(),
            }

            # url = response.urljoin(item['link'])
            # yield scrapy.Request(url=url, callback=self.parse_product, meta={'item': item})

            yield response.follow(item['link'], callback=self.parse_product, meta={'item': item})

        time.sleep(5)

        # execute with low
        yield scrapy.Request(start_url, dont_filter=True, priority=-1)

    def parse_product(self, response):
        # print('url:', response.url)

        # name = response.xpath('(//strong)[1]/text()').re(r'(\w+)')

        hp = response.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "image", " " ))] | //img').re(r':(\d+)')

        scrolls = response.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "image", " " ))] | //strong+//a//img/@title').re(r'\bScroll\b')

        for price, hp, scrolls in zip(name, hp, scrolls):
            yield {'name': name.strip(), 'hp': hp.strip(), 'scroll':scrolls.strip()}

--- 它在没有项目的情况下运行并保存在 output.csv ---

from scrapy.crawler import CrawlerRunner

def _run_crawler(spider_cls, settings):
    """
    spider_cls: Scrapy Spider class
    returns: Twisted Deferred
    """
    runner = CrawlerRunner(settings)
    return runner.crawl(spider_cls)     # return Deferred


def test_scrapy_crawler():
    deferred = _run_crawler(MySpider, settings)

    @deferred.addCallback
    def _success(results):
        """
        After crawler completes, this function will execute.
        Do your assertions in this function.
        """

    @deferred.addErrback
    def _error(failure):
        raise failure.value

    return deferred

Robots.txt

您的抓取工具正在尝试检查 robots.txt 文件,但网站上没有。

为避免这种情况,您可以在 settings.py 文件中将 ROBOTSTXT_OBEY 设置设为 false。
默认情况下它是 False 但使用 scrapy startproject 命令生成的新 scrapy 项目具有从模板生成的 ROBOTSTXT_OBEY = True

503 条回复

此外,该网站似乎在每次第一次请求时都以 503 响应。该网站正在使用某种机器人保护:

第一个请求是 503,然后正在执行一些 javascript 以发出 AJAX 生成 __shovlshield cookie 的请求:

似乎正在使用 https://shovl.io/ ddos​​ 保护。

要解决此问题,您需要逆向工程 javascript 如何生成 cookie 或使用 javascript 渲染 techniques/services 例如 selenium or splash