小编Man*_*ish的帖子

如何让Scrapy只抓取1页(使其非递归)?

我正在使用最新版本的 scrapy ( http://doc.scrapy.org/en/latest/index.html ),并试图弄清楚如何使 scrapy 仅抓取作为其一部分的 URL start_url 列表。在大多数情况下,我只想抓取 1 个页面,但在某些情况下,我可能会指定多个页面。我不希望它爬行到其他页面。

我尝试设置深度级别=1,但我不确定在测试中它是否达到了我希望达到的目标。

任何帮助将不胜感激!

谢谢你!

2015-12-22 - 代码更新:

# -*- coding: utf-8 -*-
import scrapy
from generic.items import GenericItem

class GenericspiderSpider(scrapy.Spider):
    name = "genericspider"

    def __init__(self, domain, start_url, entity_id):
        self.allowed_domains = [domain]
        self.start_urls = [start_url]
        self.entity_id = entity_id


    def parse(self, response):
        for href in response.css("a::attr('href')"):
            url = response.urljoin(href.extract())
            yield scrapy.Request(url, callback=self.parse_dir_contents)

    def parse_dir_contents(self, response):
        for sel in response.xpath("//body//a"):
            item = GenericItem()

            item['entity_id'] = self.entity_id
            # gets the actual email address …
Run Code Online (Sandbox Code Playgroud)

python scrapy

5
推荐指数
1
解决办法
1827
查看次数

标签 统计

python ×1

scrapy ×1