如何解决满足条件后,停止抓取进入下一页
我试图了解刮y的工作原理,并且想知道一旦满足条件就如何停止蜘蛛。我使用的是草率教程,以显示作者名叫Pablo Neruda的作者一旦被遗忘,那么蜘蛛就不应该继续到下一页。只要不进入下一页就可以完成抓取页面的操作。任何帮助将不胜感激。
import scrapy
class AuthorSpider(scrapy.Spider):
name = 'aq1'
start_urls = ['http://quotes.toscrape.com/']
stop_page = 0
def parse(self,response):
author_page_links = response.css('.author + a')
yield from response.follow_all(author_page_links,self.parse_author)
if AuthorSpider.stop_page == 0:
pagination_links = response.css('li.next a')
yield from response.follow_all(pagination_links,self.parse)
else:
pagination_links = " "
yield from response.follow_all(pagination_links,self.parse)
def parse_author(self,response):
def extract_with_css(query):
return response.css(query).get(default='').strip()
yield {
'Name': extract_with_css('h3.author-title::text'),}
if extract_with_css('h3.author-title::text') == "Pablo Neruda":
AuthorSpider.stop_page = 1
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。