【发布时间】:2016-01-12 05:02:34
【问题描述】:
简介 您好,我正在使用 Scrapy 来获取 Yahoo Answer 中的数据。 我的成就是将所有问题和答案都集中在一个精确的部分中。
我试试 首先使用scrapy和selenium我尝试在一个部分中列出任何问题的列表,这个列表被记住在Spider Class中。 在我使用 For 循环来解析每一页之后。
for url in self.start_urls_mod:
yield scrapy.Request(url, callback=self.parse_page)
i = i + 1
方法 parse_page 用于抓取问题页面、最佳答案和所有其他答案。 这很好用。
问题出现在我尝试使用页面右侧以下链接上的“下一个”链接中的 href 进行“下一个”问题时。 我再次调用相同的函数 parse_page,传递来自该链接的 url。 有时这项工作,但其他时候没有。 我现在不知道调用两次 parse_page 函数是否正确,而不使用其他任何基本情况来停止递归。
程序运行没有任何错误并停止,但我在“下一个”部分没有发现任何问题。只有一个人。
我的代码有一个 sn-p。
def parse_page(self, response):
#Scraping with xpath things that interests me
#Go to the next similar question
next_page = hxs.xpath('((//a[contains(@class,"Clr-b")])[3])/@href').extract()
composed_string = "https://answers.yahoo.com" + next_page[0]
print("NEXT -> "+str(composed_string))
yield scrapy.Request(urljoin(response.url, composed_string), callback=self.parse_page)
ps。我会使用乌鸦蜘蛛,但我不能定义任何规则来只接受这种类型的问题。那么请问我该如何改进我的功能。
信息: https://answers.yahoo.com/question/index?qid=20151008101821AAuHgCk
【问题讨论】:
标签: python python-2.7 selenium recursion scrapy