Python 爬行深度自动化

Python 爬行深度自动化,python,web-scraping,scrapy,Python,Web Scraping,Scrapy,我的网站包含3个级别 国家 城市 街头 我想从所有的街道网页上搜集数据。为此我造了一只蜘蛛。 现在,我如何在不在“开始URL”字段中添加一百万个URL的情况下从乡村到街道 我是否为乡村、城市和街道建造了一只蜘蛛? 爬行的整个概念不是指爬行器沿着所有链接向下移动到一定深度吗 向settings.py文件中添加DEPTH_LIMIT=3不会改变任何内容 我开始爬网的名字是:scrapy crawl spidername 编辑 您需要使用爬行爬行器,为国家、城市和街道定义 例如: fro

我的网站包含3个级别

  • 国家
    • 城市
      • 街头
我想从所有的街道网页上搜集数据。为此我造了一只蜘蛛。 现在,我如何在不在“开始URL”字段中添加一百万个URL的情况下从乡村到街道

我是否为乡村、城市和街道建造了一只蜘蛛? 爬行的整个概念不是指爬行器沿着所有链接向下移动到一定深度吗

向settings.py文件中添加DEPTH_LIMIT=3不会改变任何内容

我开始爬网的名字是:scrapy crawl spidername


编辑


您需要使用爬行爬行器,为国家、城市和街道定义

例如:

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector

class MySpider(CrawlSpider):
    name = 'example.com'
    allowed_domains = ['example.com']
    start_urls = ['http://www.example.com']

    rules = (
        Rule(SgmlLinkExtractor(allow=('country', )), callback='parse_country'),
        Rule(SgmlLinkExtractor(allow=('city', )), callback='parse_city'),
        Rule(SgmlLinkExtractor(allow=('street', )), callback='parse_street'),
    )

    def parse_country(self, response):
        self.log('Hi, this is a country page! %s' % response.url)

    def parse_city(self, response):
        self.log('Hi, this is a city page! %s' % response.url)

    def parse_street(self, response):
        self.log('Hi, this is a street page! %s' % response.url)

你说的“没有改变任何事情”是什么意思?你现在蜘蛛的行为是什么?你能和我们分享一些建议吗@alecxe关于爬行爬行器的建议可能是我添加脚本的方法,爬行器现在没有返回任何命中,当我删除规则时也没有命中,但当我将爬行爬行器更改为爬行器时,它工作正常。编辑:现在工作正常。“允许的域”中有输入错误。谢谢alecxe和paul t.谢谢!我真的需要规则吗?例如,如何编写“全部允许”命令?响应始终为0,即使我将*放在那里。@Thijs我建议使用规则只为不同类型的链接使用3个不同的回调。这就是我理解问题的方式,可能我没有理解正确。我认为你的回答非常有用,只需要稍微调整一下我的蜘蛛,一切都应该正常。
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector

class MySpider(CrawlSpider):
    name = 'example.com'
    allowed_domains = ['example.com']
    start_urls = ['http://www.example.com']

    rules = (
        Rule(SgmlLinkExtractor(allow=('country', )), callback='parse_country'),
        Rule(SgmlLinkExtractor(allow=('city', )), callback='parse_city'),
        Rule(SgmlLinkExtractor(allow=('street', )), callback='parse_street'),
    )

    def parse_country(self, response):
        self.log('Hi, this is a country page! %s' % response.url)

    def parse_city(self, response):
        self.log('Hi, this is a city page! %s' % response.url)

    def parse_street(self, response):
        self.log('Hi, this is a street page! %s' % response.url)