Подтвердить что ты не робот

Scrapy - Сканирование всего веб-сайта

Я не могу сканировать весь сайт, Scrapy просто сканирует на поверхности, я хочу глубже проползать. Был поиск в Интернете за последние 5-6 часов и никакой помощи. Мой код ниже:

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.item import Item
from scrapy.spider import BaseSpider
from scrapy import log

class ExampleSpider(CrawlSpider):
    name = "example.com"
    allowed_domains = ["example.com"]
    start_urls = ["http://www.example.com/"]
    rules = [Rule(SgmlLinkExtractor(allow=()), 
                  follow=True),
             Rule(SgmlLinkExtractor(allow=()), callback='parse_item')
    ]
    def parse_item(self,response):
        self.log('A response from %s just arrived!' % response.url)

Пожалуйста, помогите!!!!

Спасибо, Abhiram

4b9b3361

Ответ 1

Правила короткого замыкания, означающие, что первое правило, которому удовлетворяет ссылка, будет правило, которое будет применено, ваше второе правило (с обратным вызовом) не будет вызываться.

Измените свои правила:

rules = [Rule(SgmlLinkExtractor(), callback='parse_item', follow=True)]

Ответ 2

При анализе start_urls более глубокие URL-адреса могут быть проанализированы тегом href. Тогда более глубокий запрос может быть получен в функции parse(). Вот простой пример. Наиболее важный исходный код показан ниже:

from scrapy.spiders import Spider
from tutsplus.items import TutsplusItem
from scrapy.http    import Request
import re

class MySpider(Spider):
    name            = "tutsplus"
    allowed_domains = ["code.tutsplus.com"]
    start_urls      = ["http://code.tutsplus.com/"]

    def parse(self, response):
        links = response.xpath('//a/@href').extract()

        # We stored already crawled links in this list
        crawledLinks = []

        # Pattern to check proper link
        # I only want to get tutorial posts
        linkPattern = re.compile("^\/tutorials\?page=\d+")

        for link in links:
        # If it is a proper link and is not checked yet, yield it to the Spider
            if linkPattern.match(link) and not link in crawledLinks:
                link = "http://code.tutsplus.com" + link
                crawledLinks.append(link)
                yield Request(link, self.parse)

        titles = response.xpath('//a[contains(@class, "posts__post-title")]/h1/text()').extract()
        for title in titles:
            item = TutsplusItem()
            item["title"] = title
            yield item