【问题标题】:crawlSpider seems not to follow rulecrawlSpider 似乎不遵守规则
【发布时间】:2016-03-24 07:01:10
【问题描述】:

这是我的代码。实际上,我遵循了“Recursively Scraping Web Pages With Scrapy”中的示例,似乎我在某处包含了一个错误。

谁能帮我找到它,好吗?这让我发疯,我只想要所有结果页面的所有结果。相反,它给了我第 1 页的结果。

这是我的代码:

import scrapy

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

from githubScrape.items import GithubscrapeItem


class GithubSpider(CrawlSpider):
    name = "github2"
    allowed_domains = ["github.com"]

    rules = (
        Rule(SgmlLinkExtractor(allow=(), restrict_xpaths=('//*[contains(@class, "next_page")]')), callback='parse_items', follow=True),
    )

    def start_requests(self):

        baseURL = 'https://github.com/search?utf8=%E2%9C%93&q=eagle+SYSTEM+extension%3Asch+size%3A'
        for i in range(10000, 20000, +5000):
            url = baseURL+str(i+1)+".."+str(i+5000)+'&type=Code&ref=searchresults'
            print "URL:",url
            yield Request(url, callback=self.parse_items)


    def parse_items(self, response):

        hxs = Selector(response)
        resultParagraphs = hxs.xpath('//div[contains(@id,"code_search_results")]//p[contains(@class, "title")]')

        items = []
        for p in resultParagraphs:
            hrefs = p.xpath('a/@href').extract()
            projectURL = hrefs[0]
            schemeURL = hrefs[1]
            lastIndexedOn = p.xpath('.//span/time/@datetime').extract()

            i = GithubscrapeItem()
            i['counter'] = self.count
            i['projectURL'] = projectURL
            i['schemeURL'] = schemeURL
            i['lastIndexedOn'] = lastIndexedOn
            items.append(i)
        return(items)

【问题讨论】:

    标签: python-2.7 web-scraping web-crawler scrapy scrapy-spider


    【解决方案1】:

    我没有在您传递的链接上找到您的代码,但我认为问题在于您从不使用规则。

    Scrapy 通过调用 start_requests 方法开始爬行,但规则已编译并在 parse 方法上使用,您没有使用该方法,因为您的请求直接从 start_requests 发送到 parse_items

    如果您希望在该级别应用规则,可以删除 start_requests 方法上的 callback

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多