首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scrapy Spider,很难跟踪链接

Scrapy Spider,很难跟踪链接
EN

Stack Overflow用户
提问于 2017-06-22 10:18:20
回答 1查看 351关注 0票数 0

我知道有一打左右的问题与此相关,但我所看到的没有一个真正在他们的蜘蛛中有一个以上的方法……

所以我正在抓取一个网站,从分类页面开始。我抓取产品类别的链接,然后尝试利用爬虫蜘蛛的规则自动遍历每个类别中的“下一步”页面,在每一步中抓取页面中的某些信息。

问题是,我只是转到每个类别的第一个页面,似乎忽略了Rule I set的follow=True方面。所以这是代码,我需要一些帮助:

代码语言:javascript
复制
start_urls = ["http://home.mercadolivre.com.br/mais-categorias/"]

rules = (
    # I would like this to force the spider to crawl through the pages... calling the product parser each time
    Rule(LxmlLinkExtractor(allow=(),
    restrict_xpaths = '//*[@id="results-section"]/div[2]/ul/li[@class="pagination__next"]'), follow = True, callback = 'parse_product_links'),
)

def parse(self, response):
    categories = CategoriesItem()
    #categories['categoryLinks'] = []
    for link in LxmlLinkExtractor(allow=('(?<=http://lista.mercadolivre.com.br/delicatessen/)(?:whisky|licor|tequila|vodka|champagnes)'), restrict_xpaths = ("//body")).extract_links(response):
        categories['categoryURL'] = link.url
        yield Request(link.url, meta={'categoryURL': categories['categoryURL']}, callback = self.parse_product_links)


# ideally this function would grab the product links from each page
def parse_product_links(self, response):
  # I have this built out in my code, but it isnt necessary so I wanted to keep it as de-cluttered as possible

非常感谢您能提供的任何帮助,因为我似乎不完全理解如何将规则中使用的提取器用于我想要在其中使用它们的方法(这就是为什么我在两个位置使用'parse_product_links‘作为回调

EN

回答 1

Stack Overflow用户

发布于 2017-06-22 14:24:00

编写爬行爬行器规则时,请避免使用解析作为回调,因为CrawlSpider使用解析方法本身来实现其逻辑。因此,如果您覆盖了parse方法,爬行爬行器将不再工作。

来自CrawlSpider文档。

如果你不熟悉CrawlSpider的工作原理,那么使用scrapy是非常不可取的。这是一个非常隐含的捷径,可能会让人感到困惑。

在您的例子中,您覆盖了parse,这是不应该发生的,并且您只有一个下一页的规则。因此,删除parse方法并扩展规则,使其包含两个规则:查找产品规则和查找页面的规则(对于这个规则,将follow设置为True,因为您希望在新页面中查找新页面)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44689055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档