Невозможно просканировать несколько страниц

Я пытаюсь просканировать цитаты, имена авторов и теги из хорошие чтения. Я могу сканировать одну страницу со следующим кодом

import scrapy 


class goodReadsSpider(scrapy.Spider):
#identity
name='goodreads'

#requests
def start_requests(self):
    url = 'https://www.goodreads.com/quotes?page=1'
    yield scrapy.Request(url=url,callback=self.parse)


#reponse
def parse(self,response):
   for quote in response.selector.xpath('//div[@class = "quote"]'):
    yield{
        'text':quote.xpath('.//div[@class = "quoteText"]/text()[1]').extract(),
        'author':quote.xpath('.//span[@class = "authorOrTitle"]').extract_first(),
        'tags':quote.xpath('.//div[@class = "greyText smallText left"]/a/text()').extract()
    }

но когда я пытаюсь просканировать тот же паук с добавлением следующего кода

 next_page = response.selector.xpath('//a[@class = "next_page"/@href').extract()



 if next_page is not None:
           next_page_link = response.urljoin(next_page)
           yield scrapy.request(url=next_page_link, callback=self.parse)

Я получаю следующую ошибку.

2019-05-29 10:47:14 [scrapy.core.engine] INFO: Spider opened
2019-05-29 10:47:14 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min) 2019-05-29 10:47:14 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023 2019-05-29 10:47:15 [scrapy.core.engine] DEBUG: Crawled (200) https://www.goodreads.com/robots.txt> (referer: None) 2019-05-29 10:47:16 [scrapy.core.engine] DEBUG: Crawled (200) https://www.goodreads.com/quotes?page=1> (referer: None)
2019-05-29 10:47:16 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.goodreads.com/quotes?page=1> {'text': ["\n “Don't cry because it's over, smile because it happened.”\n "], 'author': '\n Dr. Seuss\n ', 'tags': ['attributed-no-source', 'cry', 'crying', 'experience', 'happiness', 'joy', 'life', 'misattributed-dr-seuss', 'optimism', 'sadness', 'smile', 'smiling']} 2019-05-29 10:47:16 [scrapy.core.scraper] ERROR: Spider error processing https://www.goodreads.com/quotes?page=1> (referer: None) Traceback (most recent call last): File "c:\programdata\anaconda3\lib\site-packages\parsel\selector.py", line 238, in xpath
**kwargs) File "src/lxml/etree.pyx", line 1586, in lxml.etree._Element.xpath File "src/lxml/xpath.pxi", line 307, in lxml.etree.XPathElementEvaluator.call File "src/lxml/xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Invalid predicate

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "c:\programdata\anaconda3\lib\site-packages\scrapy\utils\defer.py", line 102, in iter_errback
yield next(it) File "c:\programdata\anaconda3\lib\site-packages\scrapy\spidermiddlewares\offsite.py", line 29, in process_spider_output
for x in result: File "c:\programdata\anaconda3\lib\site-packages\scrapy\spidermiddlewares\referer.py", line 339, in
return (_set_referer(r) for r in result or ()) File "c:\programdata\anaconda3\lib\site-packages\scrapy\spidermiddlewares\urllength.py", line 37, in
return (r for r in result or () if _filter(r)) File "c:\programdata\anaconda3\lib\site-packages\scrapy\spidermiddlewares\depth.py", line 58, in
return (r for r in result or () if _filter(r)) File "C:\Users\Zona\Documents\Visual\demo_project\demo_project\spiders\goodreads.py", line 23, in parse
next_page = response.selector.xpath('//a[@class = "next_page"/@href').extract() File "c:\programdata\anaconda3\lib\site-packages\parsel\selector.py", line 242, in xpath
six.reraise(ValueError, ValueError(msg), sys.exc_info()[2]) File "c:\programdata\anaconda3\lib\site-packages\six.py", line 692, in reraise
raise value.with_traceback(tb) File "c:\programdata\anaconda3\lib\site-packages\parsel\selector.py", line 238, in xpath
**kwargs) File "src/lxml/etree.pyx", line 1586, in lxml.etree._Element.xpath File "src/lxml/xpath.pxi", line 307, in lxml.etree.XPathElementEvaluator.call File "src/lxml/xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._handle_result ValueError: XPath error: Invalid predicate in //a[@class = "next_page"/@href
2019-05-29 10:47:16 [scrapy.core.engine] INFO: Closing spider (finished) 2019-05-29 10:47:16 [scrapy.statscollectors] INFO: Dumping Scrapy stats: {'downloader/request_bytes': 621,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 29812, 'downloader/response_count': 2, 'downloader/response_status_count/200': 2, 'finish_reason': 'finished', 'finish_time': datetime.datetime(2019, 5, 29, 5, 47, 16, 767370), 'item_scraped_count': 1, 'log_count/DEBUG': 3,
'log_count/ERROR': 1, 'log_count/INFO': 9,
'response_received_count': 2, 'robotstxt/request_count': 1,
'robotstxt/response_count': 1,
'robotstxt/response_status_count/200': 1, 'scheduler/dequeued': 1, 'scheduler/dequeued/memory': 1, 'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1, 'spider_exceptions/ValueError': 1, 'start_time': datetime.datetime(2019, 5, 29, 5, 47, 14, 108786)}
2019-05-29 10:47:16 [scrapy.core.engine] INFO: Spider closed (finished)

Я не уверен, что проблема с xpath, потому что с первой попытки я получаю

'item_scraped_count': 30

но здесь это 1, что означает, что паук не сканирует даже первую страницу.

next_page = response.selector.xpath('//a[@class = "next_page"/@href').extract() вот ваша проблема, отсутствует ]. xpath должен быть '//a[@class = "next_page"]/@href
pawelbylina 29.05.2019 09:44
Почему в Python есть оператор "pass"?
Почему в Python есть оператор "pass"?
Оператор pass в Python - это простая концепция, которую могут быстро освоить даже новички без опыта программирования.
Некоторые методы, о которых вы не знали, что они существуют в Python
Некоторые методы, о которых вы не знали, что они существуют в Python
Python - самый известный и самый простой в изучении язык в наши дни. Имея широкий спектр применения в области машинного обучения, Data Science,...
Основы Python Часть I
Основы Python Часть I
Вы когда-нибудь задумывались, почему в программах на Python вы видите приведенный ниже код?
LeetCode - 1579. Удаление максимального числа ребер для сохранения полной проходимости графа
LeetCode - 1579. Удаление максимального числа ребер для сохранения полной проходимости графа
Алиса и Боб имеют неориентированный граф из n узлов и трех типов ребер:
Оптимизация кода с помощью тернарного оператора Python
Оптимизация кода с помощью тернарного оператора Python
И последнее, что мы хотели бы показать вам, прежде чем двигаться дальше, это
Советы по эффективной веб-разработке с помощью Python
Советы по эффективной веб-разработке с помощью Python
Как веб-разработчик, Python может стать мощным инструментом для создания эффективных и масштабируемых веб-приложений.
0
1
290
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Вам нужно исправить две проблемы, чтобы ваша ссылка на следующую страницу работала. Помимо того, что указал @pako, вы могли бы использовать .extract_first() или .get(), чтобы получить первый элемент массива. Исправленный должен быть больше похож на .xpath('//a[@class = "next_page"]/@href').get(). Я переписал некоторые из ваших xpaths, чтобы удалить пробелы из вывода.

class goodReadsSpider(scrapy.Spider):
    name='goodreads'

    start_urls = ['https://www.goodreads.com/quotes?page=1']

    def parse(self,response):
        for quote in response.xpath('//div[@class = "quote"]'):
            yield {
                'text':quote.xpath('normalize-space(.//div[@class = "quoteText"]/text())').getall(),
                'author':quote.xpath('normalize-space(.//span[@class = "authorOrTitle"]/text())').get(),
                'tags':quote.xpath('.//div[contains(@class,"greyText")]/a/text()').getall()
            }

        next_page = response.xpath('//a[@class = "next_page"]/@href').get()
        if next_page:
            nlink = response.urljoin(next_page)
            yield scrapy.Request(url=nlink,callback=self.parse)

Другие вопросы по теме