【发布时间】:2020-06-30 00:36:17
【问题描述】:
首先感谢大家的支持,这个社区给了一个老人,但是一个 Python 新手。谢谢。
我正在做一门课程,我试图理解代码的每个单词。如果有什么不清楚的地方,我会寻找答案。
我阅读了scrapy documentation,但不明白为什么使用.get() 而不是其他选项。
我正在做一个 scrapy spider。现在正在获取下一页。
问题:为什么 .get()
next_page = response.css('li.next a::attr(href)').get()
我期待
next_page = response.css('li.next a::attr(href)')
或者...
next_page = response.css('li.next a::attr(href)').extract()
这是 HTML 代码
HTML 代码只是为了清除信息。您可以访问quotes.toscrape.com
<li class="next">
<a href="/page/2/">
"Next "
<span aria-hidden="true">→</span>
</a>
</li>
这是我的完整蜘蛛代码
我认为社区不需要这个,但想尽可能提供更多信息。谢谢。
import scrapy
from ..items import QuotetutorialItem
class QuoteSpider(scrapy.Spider):
name = 'quotes'
start_urls = [
'http://quotes.toscrape.com'
]
def parse(self, response):
items = QuotetutorialItem()
all_div_quotes = response.css('div.quote')
for quotes in all_div_quotes:
title = quotes.css('span.text::text').extract()
author = quotes.css('.author::text').extract()
tag = quotes.css('.tag::text').extract()
items['title'] = title
items['author'] = author
items['tag'] = tag
yield items
next_page = response.css('li.next a::attr(href)').get()
if next_page is not None:
yield response.follow(next_page, callback= self.parse)
非常感谢。
抱歉又问了一个愚蠢的问题。
如果这篇文章对 Stack Overflow 更好,我可以删除它。
【问题讨论】:
标签: python scrapy css-selectors