Scrapy 根据条件跟踪 url
Scrapy follow urls based on condition
我正在使用 Scrapy,我想提取至少有 4 个帖子的每个主题。我有两个独立的选择器:
real_url_list 以获得每个主题的 href
nbpostsintopic_resp 获取帖子数
real_url_list = response.css("td.col-xs-8 a::attr(href)").getall()
for topic in real_url_list:
nbpostsintopic_resp = response.css("td.center ::text").get()
nbpostsintopic = nbpostsintopic_resp[0]
if int(nbpostsintopic) > 4:
yield response.follow(topic, callback=self.topic)
ULR : https://www.allodocteurs.fr/forums-et-chats/forums/allergies/allergies-aux-pollens/
不幸的是,上面没有按预期工作,帖子的数量似乎没有被考虑在内。有没有办法达到这样的条件?
提前致谢。
你的问题出在这一行
nbpostsintopic_resp = response.css("td.center ::text").get()
请注意,这将始终为您提供相同的信息,没有对您的 topic
变量的引用。
相反,遍历 tr
选择器,然后从中获取信息
def parse(self, response):
for row in response.css("tbody > tr"):
nbpostsintopic_resp = row.css("td.center::text").get()
if int(nbpostsintopic_resp) > 4:
response.follow(row.css("td > a")[0], callback=self.topic)
我正在使用 Scrapy,我想提取至少有 4 个帖子的每个主题。我有两个独立的选择器:
real_url_list 以获得每个主题的 href
nbpostsintopic_resp 获取帖子数
real_url_list = response.css("td.col-xs-8 a::attr(href)").getall() for topic in real_url_list: nbpostsintopic_resp = response.css("td.center ::text").get() nbpostsintopic = nbpostsintopic_resp[0] if int(nbpostsintopic) > 4: yield response.follow(topic, callback=self.topic)
ULR : https://www.allodocteurs.fr/forums-et-chats/forums/allergies/allergies-aux-pollens/
不幸的是,上面没有按预期工作,帖子的数量似乎没有被考虑在内。有没有办法达到这样的条件?
提前致谢。
你的问题出在这一行
nbpostsintopic_resp = response.css("td.center ::text").get()
请注意,这将始终为您提供相同的信息,没有对您的 topic
变量的引用。
相反,遍历 tr
选择器,然后从中获取信息
def parse(self, response):
for row in response.css("tbody > tr"):
nbpostsintopic_resp = row.css("td.center::text").get()
if int(nbpostsintopic_resp) > 4:
response.follow(row.css("td > a")[0], callback=self.topic)