python - 如何向蜘蛛提供在蜘蛛内爬行的链接?

标签 python scrapy

我正在为一家在线商店编写一个蜘蛛程序 (CrawlSpider)。根据客户的要求,我需要编写两个规则:一个用于确定哪些页面有项目,另一个用于提取项目。

我的两条规则已经独立运行:

  • 如果我的 start_urls = ["www.example.com/books.php", "www.example.com/movies.php"] 并评论 Rule 和代码 parse_category 中,我的 parse_item 将提取每个项目。
  • 另一方面,如果 start_urls = "http://www.example.com" 并且我 注释Ruleparse_item的代码,parse_category将 返回其中有要提取的项目的每个链接,即 parse_category 将返回 www.example.com/books.php 并且 www.example.com/movies.php

我的问题是我不知道如何合并两个模块,因此 start_urls = "http://www.example.com" 然后 parse_category提取 www.example.com/books.phpwww.example.com/movies.php 并将这些链接提供给 parse_item,我在其中实际上提取每个项目的信息。

我需要找到一种方法来做到这一点,而不是仅仅使用 start_urls = ["www.example.com/books.php", "www.example.com/movies.php"] 因为如果将来添加新类别(例如 www.example.com/music.php),蜘蛛将无法自动检测到该新类别,应手动编辑。没什么大不了的,但客户不想要这个。

class StoreSpider (CrawlSpider):
    name = "storyder"

    allowed_domains = ["example.com"]
    start_urls = ["http://www.example.com/"]
    #start_urls = ["http://www.example.com/books.php", "http://www.example.com/movies.php"]

    rules = (
        Rule(LinkExtractor(), follow=True, callback='parse_category'),
        Rule(LinkExtractor(), follow=False, callback="parse_item"),
    )

def parse_category(self, response):
    category = StoreCategory()
    # some code for determining whether the current page is a category, or just another stuff 
    if is a category:
        category['name'] = name
        category['url'] = response.url
    return category

def parse_item(self, response):
    item = StoreItem()
    # some code for extracting the item's data
    return item

最佳答案

CrawlSpider 规则无法按您想要的方式工作,您需要自己实现逻辑。当您指定 follow=True 时,您不能使用回调,因为其想法是在遵循规则的同时不断获取链接(无项目),请检查 documentation

你可以尝试这样的事情:

class StoreSpider (CrawlSpider):
    name = "storyder"

    allowed_domains = ["example.com"]
    start_urls = ["http://www.example.com/"]
    # no rules
def parse(self, response): # this is parse_category
    category_le = LinkExtractor("something for categories")
    for a in category_le.extract_links(response):
        yield Request(a.url, callback=self.parse_category)
    item_le = LinkExtractor("something for items")
    for a in item_le.extract_links(response):
        yield Request(a.url, callback=self.parse_item)
def parse_category(self, response):
    category = StoreCategory()
    # some code for determining whether the current page is a category, or just another stuff 
    if is a category:
        category['name'] = name
        category['url'] = response.url
        yield category
    for req in self.parse(response):
        yield req
def parse_item(self, response):
    item = StoreItem()
    # some code for extracting the item's data
    return item

关于python - 如何向蜘蛛提供在蜘蛛内爬行的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33469129/

相关文章:

python - 为什么我的 Scrapy 代码返回一个空数组?

python - scrapy:蜘蛛在所有请求产生之前退出,没有错误消息

javascript - 使用 scrapy 从动态网站复制数据

python - pymongo 问题 : TypeError: document must be an instance of dict, bson.son.SON

python - 处理具有多个权重的网络中的内存问题

python - 附加到 for 循环内的 numpy 数组或列表 - 哪个更可取?

python - Scrapy 管道以正确的格式导出 csv 文件

python - Flask 中的代码 503 与嵌入式 Bokeh 服务器应用程序通过 requests.get() 获取 json 化数据

python - 指定安装分发/设置工具包的 'tests_require' 依赖项的位置

python - 无法导入 Scrapy 的设置模块或其 scrapy.cfg