避免错页

2024-09-26 18:07:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经发布了上周,我的机器人总是在321页封锁。我更改了Scrapy设置,但注意到321和结尾之间的页面似乎不包含任何项。你知道吗

我想知道如何跳过产生错误的页面。我试过这个:

    next_pages = response.xpath("//div[@class='pgLightPrevNext']/a/@href").extract() #essai pour accéder au contenu des pages suivantes
    for next in next_pages:
        absolute_url = self.base_url + next
        try:
            yield scrapy.Request(absolute_url, callback=self.parse_dir_contents)
        except:
            pass

但没有结果。我怎么能跳过那些页?你知道吗

谢谢。你知道吗


Tags: selfdivurlresponse错误结尾机器人页面
2条回答

在下一个获取数据的函数中,检查response==200。 如果响应不等于200,则可以在特定限制下使用retry变量,使用另一个函数重试该url。如果超过了限制,请转到下一个产品url。你知道吗

try:
        if response.status == 404:
            self.append(self.bad_log_file, response.url)
            self.append(self.fourohfour, response.url)

        elif response.status == 200:
            self.append(self.ok_log_file, response.url)
        else:
            self.append(self.bad_log_file, response.url)

    except Exception, e:
        self.log('[eccezione] : %s' % e)
        pass

如果为页面收集的项目数为0,则可以return。你知道吗

相关问题 更多 >

    热门问题