首页 > 解决方案 > 使用scrapy、规则和链接提取器抓取“旧”页面

问题描述

我一直在用scrapy做一个项目。在这个可爱的社区的帮助下,我设法抓取了这个网站的第一页:http ://www.rotoworld.com/playernews/nfl/football-player-news?ls=roto%3anfl%3agnav 。我也在尝试从“旧”页面中抓取信息。我研究了“crawlspider”、规则和链接提取器,并相信我有正确的代码。我希望蜘蛛在后续页面上执行相同的循环。不幸的是,在我运行它的那一刻,它只是吐出第一页,并没有继续到“旧”页。

我不确定我需要改变什么,并且非常感谢一些帮助。有些帖子可以追溯到 2004 年 2 月……我是数据挖掘的新手,不确定是否真的可以抓取每篇帖子。如果是我愿意。请任何帮助表示赞赏。谢谢!

import scrapy
from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.contrib.linkextractors import LinkExtractor



class Roto_News_Spider2(crawlspider):
    name = "RotoPlayerNews"

    start_urls = [
        'http://www.rotoworld.com/playernews/nfl/football/',
    ]

    Rules = (Rule(LinkExtractor(allow=(), restrict_xpaths=('//input[@id="cp1_ctl00_btnNavigate1"]',)), callback="parse_page", follow= True),)


    def parse(self, response):
        for item in response.xpath("//div[@class='pb']"):
            player = item.xpath(".//div[@class='player']/a/text()").extract_first()
            position= item.xpath(".//div[@class='player']/text()").extract()[0].replace("-","").strip()
            team = item.xpath(".//div[@class='player']/a/text()").extract()[1].strip()
            report = item.xpath(".//div[@class='report']/p/text()").extract_first()
            date = item.xpath(".//div[@class='date']/text()").extract_first() + " 2018"
            impact = item.xpath(".//div[@class='impact']/text()").extract_first().strip()
            source = item.xpath(".//div[@class='source']/a/text()").extract_first()
            yield {"Player": player,"Position": position, "Team": team,"Report":report,"Impact":impact,"Date":date,"Source":source}

标签: web-scrapingscrapyrules

解决方案


我的建议:硒

如果要自动更改页面,可以使用Selenium WebDriverSelenium使您能够与页面进行交互,点击按钮,写入输入等。您需要更改代码以废弃data然后点击older按钮。然后,它会更改页面并继续抓取。

Selenium是一个非常有用的工具。我现在正在使用它,用于个人项目。您可以查看我在 GitHub 上的 repo以了解它是如何工作的。对于您要废弃的页面,您不能只更改 to 就更旧linkscraped因此,您需要使用Selenium在页面之间进行更改。

希望能帮助到你。


推荐阅读