如何克服Scrapy -调试:爬网(520)问题?

hm2xizp9  于 2023-01-26  发布在  其他
关注(0)|答案(1)|浏览(199)

我刚刚从THIS问题(也在THIS repo中提到)中编写了Scrapy spider。看起来它在一年前工作,但现在book24.ru阻止了spider并返回520错误。
Shell响应520错误:

2023-01-20 22:10:23 [scrapy.utils.log] INFO: Scrapy 2.7.1 started (bot: bestsellers)
2023-01-20 22:10:23 [scrapy.utils.log] INFO: Versions: lxml 4.9.2.0, libxml2 2.9.14, cssselect 1.2.0, parsel 1.7.0, w3lib 2.1.1, Twisted 22.10.0, Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0], pyOpenSSL 23.0.0 (OpenSSL 3.0.7 1 Nov 2022), cryptography 39.0.0, Platform Linux-5.15.0-58-generic-x86_64-with-glibc2.35
2023-01-20 22:10:23 [scrapy.crawler] INFO: Overridden settings:
{'BOT_NAME': 'bestsellers',
 'NEWSPIDER_MODULE': 'bestsellers.spiders',
 'REQUEST_FINGERPRINTER_IMPLEMENTATION': '2.7',
 'SPIDER_MODULES': ['bestsellers.spiders'],
 'TWISTED_REACTOR': 'twisted.internet.asyncioreactor.AsyncioSelectorReactor'}
2023-01-20 22:10:23 [asyncio] DEBUG: Using selector: EpollSelector
2023-01-20 22:10:23 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.asyncioreactor.AsyncioSelectorReactor
2023-01-20 22:10:23 [scrapy.utils.log] DEBUG: Using asyncio event loop: asyncio.unix_events._UnixSelectorEventLoop
2023-01-20 22:10:23 [scrapy.extensions.telnet] INFO: Telnet Password: cb384b87b1e215df
2023-01-20 22:10:23 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.memusage.MemoryUsage',
 'scrapy.extensions.feedexport.FeedExporter',
 'scrapy.extensions.logstats.LogStats']
2023-01-20 22:10:23 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2023-01-20 22:10:23 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2023-01-20 22:10:23 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2023-01-20 22:10:23 [scrapy.core.engine] INFO: Spider opened
2023-01-20 22:10:23 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2023-01-20 22:10:23 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2023-01-20 22:10:23 [filelock] DEBUG: Attempting to acquire lock 140382385887728 on /home/user/.cache/python-tldextract/3.10.6.final__first_scrapy-rekTiTXv__400173__tldextract-3.4.0/publicsuffix.org-tlds/de84b5ca2167d4c83e38fb162f2e8738.tldextract.json.lock
2023-01-20 22:10:23 [filelock] DEBUG: Lock 140382385887728 acquired on /home/user/.cache/python-tldextract/3.10.6.final__first_scrapy-rekTiTXv__400173__tldextract-3.4.0/publicsuffix.org-tlds/de84b5ca2167d4c83e38fb162f2e8738.tldextract.json.lock
2023-01-20 22:10:23 [filelock] DEBUG: Attempting to release lock 140382385887728 on /home/user/.cache/python-tldextract/3.10.6.final__first_scrapy-rekTiTXv__400173__tldextract-3.4.0/publicsuffix.org-tlds/de84b5ca2167d4c83e38fb162f2e8738.tldextract.json.lock
2023-01-20 22:10:23 [filelock] DEBUG: Lock 140382385887728 released on /home/user/.cache/python-tldextract/3.10.6.final__first_scrapy-rekTiTXv__400173__tldextract-3.4.0/publicsuffix.org-tlds/de84b5ca2167d4c83e38fb162f2e8738.tldextract.json.lock
2023-01-20 22:10:23 [scrapy.core.engine] DEBUG: Crawled (520) <GET https://book24.ru/product/transhumanism-inc-6015821/> (referer: None)
2023-01-20 22:10:23 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <520 https://book24.ru/product/transhumanism-inc-6015821/>: HTTP status code is not handled or not allowed
2023-01-20 22:10:23 [scrapy.core.engine] INFO: Closing spider (finished)
2023-01-20 22:10:23 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 243,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 7640,
 'downloader/response_count': 1,
 'downloader/response_status_count/520': 1,
 'elapsed_time_seconds': 0.364996,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2023, 1, 20, 19, 10, 23, 800623),
 'httperror/response_ignored_count': 1,
 'httperror/response_ignored_status_count/520': 1,
 'log_count/DEBUG': 8,
 'log_count/INFO': 11,
 'memusage/max': 65613824,
 'memusage/startup': 65613824,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2023, 1, 20, 19, 10, 23, 435627)}
2023-01-20 22:10:23 [scrapy.core.engine] INFO: Spider closed (finished)

第一次尝试-尝试添加headers cause HERE,因为有些网站会阻止robot请求,您应该添加标题,使自己看起来像是使用Web浏览器的普通用户。但它没有帮助,并再次返回520错误。

import scrapy

class BookSpider(scrapy.Spider):
    name = 'book24-2'
    start_urls = ['https://book24.ru/knigi-bestsellery/']
    
    def parse(self, response):
        headers = {
        'user-agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
        }
        
        for link in response.css('div.product-card__image-holder a::attr(href)', headers=headers).get():
            yield response.follow(link, callback=self.parse_book, headers=headers)

        for i in range(1, 25):
            next_page = f'https://book24.ru/knigi-bestsellery/page-{i}/'
            yield response.follow(next_page, callback=self.parse, headers=headers)

    def parse_book(self, response):
        yield{
            'name': response.css('h1.product-detail-page__title::text').get(),
            'buy': response.css('p.product-detail-page__purchased-text::text').get().split()[1],
            'type': response.css('div.product-characteristic__value a::attr(title)')[2].get()
        }

第二次尝试-尝试在www.example.com中将ROBOTSTXT_OBEY = True更改为x1m2 n1settings.py- 520错误仍然存在。
第三次尝试-尝试使用fake_useragent lib -无效。代码:

import scrapy
from fake_useragent import UserAgent

# print(scrapy.Request('https://book24.ru/knigi-bestsellery/'))

class BookSpider(scrapy.Spider):
    name = 'book24-2'
    start_urls = ['https://book24.ru/knigi-bestsellery/']
    
    # self значит, что остаемся в пределах этого класса
    # response это ответ, который видели в scrapy shell, к примеру, response.css('...')
    def parse(self, response):
        # headers = {
        # 'user-agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
        # }
        headers = {
        'user-agent' : UserAgent().random
        }
        
        for link in response.css('div.product-card__image-holder a::attr(href)', headers=headers).get():
            # yield - ключевое слово в scrapy, которое позволяет выполнять разные действия
            # yield scrapy.Request('https://book24.ru/knigi-bestsellery/', headers=headers)
            # print('++++++++++++'+response)
            yield response.follow(link, callback=self.parse_book, headers=headers)

        for i in range(1, 25):
            next_page = f'https://book24.ru/knigi-bestsellery/page-{i}/'
            yield response.follow(next_page, callback=self.parse, headers=headers)

    def parse_book(self, response):
        headers = {
        'user-agent' : UserAgent().random
        }
        
        yield{
            'name': response.css('h1.product-detail-page__title::text', headers=headers).get(),
            'buy': response.css('p.product-detail-page__purchased-text::text', headers=headers).get().split()[1],
            'type': response.css('div.product-characteristic__value a::attr(title)', headers=headers)[2].get()
        }
iih3973s

iih3973s1#

您的spider代码有多个问题需要解决。
1.你的css选择器有一个关键字参数头,这是防止你从爬行的书页。

for link in response.css('div.product-card__image-holder a::attr(href)', headers=headers).get():

1.据我所知,只有14页的书,你有你的for循环范围设置为25。

for i in range(1, 25):

1.你的'type'字段在你的项目是寻址一个特定的索引,然而,不是所有的图书特征是统一的格式,所以在超过一半的图书页面,你试图刮取的元素不存在.

'type': response.css('div.product-characteristic__value a::attr(title)')[2].get()

所有这些都是非常简单的修复方法。对于type字段,您可以简单地迭代特征标签,直到到达您正在寻找的标签,然后提取信息并中断迭代。
例如:

import scrapy

class BookSpider(scrapy.Spider):
    name = 'book24-2'
    start_urls = ['https://book24.ru/knigi-bestsellery/']
    custom_settings = {
    "ROBOTSTXT_OBEY": False,
    "USER_AGENT": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
    }

    def parse(self, response):
        for link in response.css('div.product-card__image-holder a::attr(href)').getall():
            yield response.follow(link, callback=self.parse_book)

        for i in range(1, 14):
            next_page = f'https://book24.ru/knigi-bestsellery/page-{i}/'
            yield response.follow(next_page, callback=self.parse)

    def parse_book(self, response):
        item = {
            'name': response.css('h1.product-detail-page__title::text').get(),
            'buy': response.css(
                'p.product-detail-page__purchased-text::text').get().split()[1]}
        for trait in response.css('div.product-characteristic__item'):
            label = trait.xpath('./dt/span/text()').get()
            if "Серия:" in label:   # <- change the label to the one you are looking for
                item["type"] = trait.xpath('./dd/a/@title').get()
                break
        yield item

运行上面的代码,这是我输出的一部分。

{'name': ' Гонки химер ', 'buy': '738', 'type': None}
2023-01-20 13:14:16 [scrapy.core.scraper] DEBUG: Scraped from <200 https://book24.ru/product/sobaka-baskerviley-5375698/>
{'name': ' Дойл Артур Конан: Собака Баскервилей ', 'buy': '1063', 'type': 'Классика на пятерочку!'}
2023-01-20 13:14:16 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://book24.ru/product/effektivnaya-biznes-kommunikatsiya-volshebnye-tabletki-dlya-delovykh-lyudey-5401347/> (referer: https://book24.ru/knigi-
bestsellery/)
2023-01-20 13:14:16 [scrapy.core.scraper] DEBUG: Scraped from <200 https://book24.ru/product/effektivnaya-biznes-kommunikatsiya-volshebnye-tabletki-dlya-delovykh-lyudey-5401347/>
{'name': ' Северская Ольга Игоревна, Селезнева Лариса Васильевна: Эффективная бизнес-коммуникация. "Волшебные таблетки" для деловых людей ', 'buy': '998', 'type': 'Тотальный диктант. Фонд поддержки языковой культу
ры граждан'}
2023-01-20 13:14:16 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://book24.ru/product/chto-budet-dalshe-iskusstvo-prevrashchat-istorii-v-stsenarii-5956711/> (referer: https://book24.ru/knigi-bestsellery/)
2023-01-20 13:14:17 [scrapy.core.scraper] DEBUG: Scraped from <200 https://book24.ru/product/chto-budet-dalshe-iskusstvo-prevrashchat-istorii-v-stsenarii-5956711/>
{'name': ' Что будет дальше? Искусство превращать истории в сценарии ', 'buy': '1095', 'type': 'Мастер сцены'}
2023-01-20 13:14:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://book24.ru/product/predvestnik-zemletryaseniya-5449404/> (referer: https://book24.ru/knigi-bestsellery/)
2023-01-20 13:14:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://book24.ru/product/tolko-esli-ty-zakhochesh-5406995/> (referer: https://book24.ru/knigi-bestsellery/)
2023-01-20 13:14:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://book24.ru/product/opyty-bespriyutnogo-neba-5455029/> (referer: https://book24.ru/knigi-bestsellery/)
2023-01-20 13:14:17 [scrapy.core.scraper] DEBUG: Scraped from <200 https://book24.ru/product/predvestnik-zemletryaseniya-5449404/>
{'name': ' Джонс Сюзанна: Предвестник землетрясения ', 'buy': '1131', 'type': 'Кинопремьера мирового масштаба'}
2023-01-20 13:14:17 [scrapy.core.scraper] DEBUG: Scraped from <200 https://book24.ru/product/tolko-esli-ty-zakhochesh-5406995/>
{'name': ' Комб Брюно: Только если ты захочешь ', 'buy': '771', 'type': 'Самый сильный наркотик — любовь'}
2023-01-20 13:14:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://book24.ru/product/klub-samoubiyts-5409602/> (referer: https://book24.ru/knigi-bestsellery/)
2023-01-20 13:14:17 [scrapy.core.scraper] DEBUG: Scraped from <200 https://book24.ru/product/opyty-bespriyutnogo-neba-5455029/>
{'name': ' Гаврилов Степан Егорович: Опыты бесприютного неба ', 'buy': '667', 'type': 'Вперед и вверх. Современная проза'}
2023-01-20 13:14:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://book24.ru/product/esli-by-eti-krylya-mogli-letat-5477816/> (referer: https://book24.ru/knigi-bestsellery/)
2023-01-20 13:14:17 [scrapy.core.scraper] DEBUG: Scraped from <200 https://book24.ru/product/klub-samoubiyts-5409602/>
{'name': ' Роберт Льюис Стивенсон: Клуб самоубийц ', 'buy': '3842', 'type': 'Эксклюзивная классика'}
2023-01-20 13:14:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://book24.ru/product/rabota-nad-oshibkoy-5358564/> (referer: https://book24.ru/knigi-bestsellery/)
2023-01-20 13:14:17 [scrapy.core.scraper] DEBUG: Scraped from <200 https://book24.ru/product/esli-by-eti-krylya-mogli-letat-5477816/>
{'name': ' Если бы эти крылья могли летать ', 'buy': '1424', 'type': 'Young Adult. Важные книги для молодёжи'}
2023-01-20 13:14:17 [scrapy.core.scraper] DEBUG: Scraped from <200 https://book24.ru/product/rabota-nad-oshibkoy-5358564/>
{'name': ' Петерсен Орсон: Работа над ошибкой ', 'buy': '967', 'type': 'Авангард. Современная интеллектуальная фантастика'}
2023-01-20 13:14:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://book24.ru/product/upravlenie-vnimaniem-6660752/> (referer: https://book24.ru/knigi-bestsellery/)
2023-01-20 13:14:17 [scrapy.core.scraper] DEBUG: Scraped from <200 https://book24.ru/product/upravlenie-vnimaniem-6660752/>
{'name': ' Король Александр: Управление вниманием ', 'buy': '675', 'type': 'Книги, улучшающие жизнь'}
2023-01-20 13:14:17 [scrapy.core.engine] INFO: Closing spider (finished)
2023-01-20 13:14:17 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 175161,
 'downloader/request_count': 405,
 'downloader/request_method_count/GET': 405,
 'downloader/response_bytes': 12608278,
 'downloader/response_count': 405,
 'downloader/response_status_count/200': 404,
 'downloader/response_status_count/301': 1,
 'dupefilter/filtered': 199,
 'elapsed_time_seconds': 56.672512,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2023, 1, 20, 21, 14, 17, 584866),
 'httpcompression/response_bytes': 52230852,
 'httpcompression/response_count': 404,
 'item_scraped_count': 390,   # <--- results
 'log_count/DEBUG': 801,
 'log_count/INFO': 10,
 'request_depth_max': 2,
 'response_received_count': 404,
 'scheduler/dequeued': 405,
 'scheduler/dequeued/memory': 405,
 'scheduler/enqueued': 405,
 'scheduler/enqueued/memory': 405,
 'start_time': datetime.datetime(2023, 1, 20, 21, 13, 20, 912354)}
2023-01-20 13:14:17 [scrapy.core.engine] INFO: Spider closed (finished)

如您所见,蜘蛛生成了390个项目。

相关问题