1

CrawlSpider を拡張するスパイダーを作成し、http: //scrapy.readthedocs.org/en/latest/topics/spiders.html のアドバイスに従いました。

問題は、開始 URL (たまたまホスト名と一致する) とそれが含むいくつかのリンクの両方を解析する必要があることです。

だから私は次のようなルールを定義しました: rules = [Rule(SgmlLinkExtractor(allow=['/page/d+']), callback='parse_items', follow=True)]、しかし何も起こりません。

次に、次のような一連のルールを定義しようとしましたrules = [Rule(SgmlLinkExtractor(allow=['/page/d+']), callback='parse_items', follow=True), Rule(SgmlLinkExtractor(allow=['/']), callback='parse_items', follow=True)]。問題は、スパイダーがすべてを解析することです。

スパイダーに _start_url_ とそれに含まれるいくつかのリンクのみを解析するように指示するにはどうすればよいですか?

アップデート:

メソッドをオーバーライドしようとしたparse_start_urlので、開始ページからデータを取得できるようになりましたが、次のように定義されたリンクにはまだ従いませんRule:

class ExampleSpider(CrawlSpider):
  name = 'TechCrunchCrawler'
  start_urls = ['http://techcrunch.com']
  allowed_domains = ['techcrunch.com']
  rules = [Rule(SgmlLinkExtractor(allow=['/page/d+']), callback='parse_links', follow=True)]

  def parse_start_url(self, response):
      print '++++++++++++++++++++++++parse start url++++++++++++++++++++++++'
      return self.parse_links(response)

  def parse_links(self, response):
      print '++++++++++++++++++++++++parse link called++++++++++++++++++++++++'
      articles = []
      for i in HtmlXPathSelector(response).select('//h2[@class="headline"]/a'):
          article = Article()
          article['title'] = i.select('./@title').extract()
          article['link'] = i.select('./@href').extract()
          articles.append(article)

      return articles
4

2 に答える 2

1

あなたはバックスラッシュを忘れました-文字dを次のようにエスケープします\d

>>> SgmlLinkExtractor(allow=r'/page/d+').extract_links(response)
[]
>>> SgmlLinkExtractor(allow=r'/page/\d+').extract_links(response)
[Link(url='http://techcrunch.com/page/2/', text=u'Next Page',...)]
于 2012-07-27T15:01:34.253 に答える
1

私は過去に同様の問題を抱えていました。
私はBaseSpiderにこだわりました。

これを試して:

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
from scrapy.contrib.loader import XPathItemLoader

from techCrunch.items import Article


class techCrunch(BaseSpider):
    name = 'techCrunchCrawler'
    allowed_domains = ['techcrunch.com']

    # This gets your start page and directs it to get parse manager
    def start_requests(self):
        return [Request("http://techcrunch.com", callback=self.parseMgr)]

    # the parse manager deals out what to parse and start page extraction
    def parseMgr(self, response):
        print '++++++++++++++++++++++++parse start url++++++++++++++++++++++++'
        yield self.pageParser(response)

        nextPage = HtmlXPathSelector(response).select("//div[@class='page-next']/a/@href").extract()
        if nextPage:
            yield Request(nextPage[0], callback=self.parseMgr)

    # The page parser only parses the pages and returns items on each page call
    def pageParser(self, response):
        print '++++++++++++++++++++++++parse link called++++++++++++++++++++++++'
        loader = XPathItemLoader(item=Article(), response=response)
        loader.add_xpath('title', '//h2[@class="headline"]/a/@title')
        loader.add_xpath('link', '//h2[@class="headline"]/a/@href')
        return loader.load_item()
于 2012-07-26T20:53:52.033 に答える