创建请求的快捷方式¶作为创建请求对象的快捷方式,您可以使用 import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'http://quotes.toscrape.com/page/1/',
]
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').get(),
'author': quote.css('span small::text').get(),
'tags': quote.css('div.tags a.tag::text').getall(),
}
next_page = response.css('li.next a::attr(href)').get()
if next_page is not None:
yield response.follow(next_page, callback=self.parse)
不像Scrapy.Request, 也可以将选择器传递给 for href in response.css('ul.pager a::attr(href)'):
yield response.follow(href, callback=self.parse)
为了 for a in response.css('ul.pager a'):
yield response.follow(a, callback=self.parse)
要从iterable创建多个请求,可以使用 anchors = response.css('ul.pager a')
yield from response.follow_all(anchors, callback=self.parse)
或者,进一步缩短: yield from response.follow_all(css='ul.pager a', callback=self.parse) |
Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )
GMT+8, 2024-12-4 01:49 , Processed in 0.135894 second(s), 17 queries .