第一页不抓取,下一页遵循规则




我用递归规则做了一个蜘蛛来跟踪下一页的链接。它运行良好
蜘蛛爬行除"start_urls"中定义的第一个页面外的所有页面(999(。有人遇到过这个问题吗?

示例代码:

class example(CrawlSpider):
    name = "example"
    allowed_domains = ["example.ndd"]
    start_urls = ["http://example.ndd/startnum=1"] #first page  
    rules = (Rule(SgmlLinkExtractor(allow=("nextPage.htm", ),
                            restrict_xpaths=('//div[@class="paging"]/p[@class="nav"]',)), 
                            callback="parse_items", follow= True),)
    def parse_items(self, response):
        hxs = HtmlXPathSelector(response)
        links= hxs.select('//td[@class="desc"]/h3/a/@href').extract()
        list = []
        for link in links:
            yield link
        return

编辑:搜索后,爬虫会直接按照规则限制路径的结果进行搜索。因此,如果从startnum=1开始,它也会抓取startnum=1页面内的startnum=11
我不知道如何解决这个问题。

您需要使用:def parse_start_url(self, response):

看到这个答案:Scrapy CrawlSpider没有';t爬网第一个登录页

您的意思是要爬网start_urls并提取它以及其他页面吗?

CrawlSpider不会从start_urls中提取项目。如果需要,请重载parse方法。

最新更新