【问题标题】:Scrapy -- Scraping a page and scraping next pagesScrapy -- 抓取一页并抓取下一页
【发布时间】:2018-02-03 12:22:46
【问题描述】:

我正在尝试为我的 items.py 文件中定义的教授统计数据抓取 RateMyProfessors:

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html

from scrapy.item import Item, Field


class ScraperItem(Item):
    # define the fields for your item here like:
    numOfPages = Field() # number of pages of professors (usually 476)

    firstMiddleName = Field() # first (and middle) name
    lastName = Field() # last name
    numOfRatings = Field() # number of ratings
    overallQuality = Field() # numerical rating
    averageGrade = Field() # letter grade
    profile = Field() # url of professor profile

    pass

这是我的 scraper_spider.py 文件:

import scrapy

from scraper.items import ScraperItem
from scrapy.contrib.spiders import Rule
from scrapy.contrib.linkextractors import LinkExtractor


class scraperSpider(scrapy.Spider):
    name = "scraper"
    allowed_domains = ["www.ratemyprofessors.com"]
    start_urls = [
    "http://www.ratemyprofessors.com/search.jsp?queryBy=teacherName&schoolName=pennsylvania+state+university"
    ]

    rules = (
        Rule(LinkExtractor(restrict_xpaths=('//a[@class="nextLink"]')),callback='parse',follow=True),
        )

    def parse(self, response):
        # professors = []
        numOfPages = int(response.xpath('((//a[@class="step"])[last()])/text()').extract()[0])

        # create array of profile links
        profiles = response.xpath('//li[@class="listing PROFESSOR"]/a/@href').extract()

        # for each of those links
        for profile in profiles:
            # define item
            professor = ScraperItem();

            # add profile to professor
            professor["profile"] = profile

            # pass each page to the parse_profile() method
            request = scrapy.Request("http://www.ratemyprofessors.com"+profile,
                 callback=self.parse_profile)
            request.meta["professor"] = professor

            # add professor to array of professors
            yield request


    def parse_profile(self, response):
        professor = response.meta["professor"]

        if response.xpath('//*[@class="pfname"]'):
            # scrape each item from the link that was passed as an argument and add to current professor
            professor["firstMiddleName"] = response.xpath('//h1[@class="profname"]/span[@class="pfname"][1]/text()').extract() 

        if response.xpath('//*[@class="plname"]'):
            professor["lastName"] = response.xpath('//h1[@class="profname"]/span[@class="plname"]/text()').extract()

        if response.xpath('//*[@class="table-toggle rating-count active"]'):
            professor["numOfRatings"] = response.xpath('//div[@class="table-toggle rating-count active"]/text()').extract()

        if response.xpath('//*[@class="grade"]'):
            professor["overallQuality"] = response.xpath('//div[@class="breakdown-wrapper"]/div[@class="breakdown-header"][1]/div[@class="grade"]/text()').extract()

        if response.xpath('//*[@class="grade"]'):
            professor["averageGrade"] = response.xpath('//div[@class="breakdown-wrapper"]/div[@class="breakdown-header"][2]/div[@class="grade"]/text()').extract()

        return professor

# add string to rule.  linkextractor only gets "/showratings.." not "ratemyprofessors.com/showratings"

我的问题在于上面的 scraper_spider.py 文件。蜘蛛应该去thisRateMyProfessors 页面并转到每个教授并获取信息,然后返回目录并获取下一位教授的信息。在页面上没有教授要抓取后,它应该找到下一步按钮href值,然后转到该页面并按照相同的方法进行操作。

我的刮刀可以刮掉目录第 1 页上的所有教授,但之后就停止了,因为它不会转到下一页。

你能帮助我的爬虫成功找到并转到下一页吗?

我尝试关注this StackOverflow 问题,但它太具体而无法使用。

【问题讨论】:

    标签: python web-scraping scrapy scrapy-spider


    【解决方案1】:

    如果您想使用rules 属性,您的scraperSpider 应该继承自CrawlSpider。请参阅文档here。另请注意文档中的此警告

    在编写爬虫规则时,避免使用 parse 作为回调,因为 CrawlSpider 使用 parse 方法本身来实现其逻辑。 所以如果你重写 parse 方法,爬虫将不再 工作。

    【讨论】:

    • 感谢您的回复。我很抱歉,因为我一直很忙,所以迟到了几天。您会推荐使用其他任何功能吗?我需要使用parse() 方法抓取下一页。我按照您的建议导入了CrawlSpider,但它不会继续并抓取下一页,而是将第一页中的相同教授转储到我的 JSON 文件中 10 次。有什么建议吗?
    【解决方案2】:

    我通过忽略所有规则并遵循this docFollowing links部分解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2015-12-13
      • 2015-05-02
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多