私はサイトからいくつかのページ区切りのページデータを抽出を通じて横断スパイダー構築しています:これは蜘蛛です http://www.usnews.com/education/best-global-universities/neuroscience-behaviorScrapyリュエルLinkExtractor次のページ
を:
# -*- coding: utf-8 -*-
import scrapy
from scrapy.contrib.spiders import Rule
from scrapy.linkextractors import LinkExtractor
from lxml import html
from usnews.items import UsnewsItem
class UniversitiesSpider(scrapy.Spider):
name = "universities"
allowed_domains = ["usnews.com"]
start_urls = (
'http://www.usnews.com/education/best-global-universities/neuroscience-behavior/',
)
#Rules = [
#Rule(LinkExtractor(allow=(), restrict_xpaths=('.//a[@class="pager_link"]',)), callback="parse", follow= True)
#]
def parse(self, response):
for sel in response.xpath('.//div[@class="sep"]'):
item = UsnewsItem()
item['name'] = sel.xpath('.//h2[@class="h-taut"]/a/text()').extract()
item['location'] = sel.xpath('.//span[@class="t-dim t-small"]/text()').extract()
item['ranking'] = sel.xpath('.//div[3]/div[2]/text()').extract()
item['score'] = sel.xpath('.//div[@class="t-large t-strong t-constricted"]/text()').extract()
#print(sel.xpath('.//text()').extract()
yield item
ページネーションを横断するためのルールコードが最初のサイトのデータを吐き出すだけなので、何もしていないようです。スパイダーが15ページすべてを通過し、サイトから4つのアイテム(名前、場所、ランキング、得点)を抽出するようにルールを正しく実装するにはどうすればよいですか?
'rules'属性(あなたのコードのような" Rules "ではなく" rules "のように)を使うには、' scrapy.Spider.Spider'ではなく 'scrapy.CrawlSpider'をサブクラス化する必要があります。そして、@steveが彼の答えで言うように、 'CrawlSpider'の' parse'メソッドを再定義するべきではありません。 –