web-scraping - 使用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}
解决方案
我的建议:硒
如果要自动更改页面,可以使用Selenium WebDriver。
Selenium
使您能够与页面进行交互,点击按钮,写入输入等。您需要更改代码以废弃data
然后点击older
按钮。然后,它会更改页面并继续抓取。
Selenium
是一个非常有用的工具。我现在正在使用它,用于个人项目。您可以查看我在 GitHub 上的 repo以了解它是如何工作的。对于您要废弃的页面,您不能只更改 to 就更旧link
,scraped
因此,您需要使用Selenium
在页面之间进行更改。
希望能帮助到你。
推荐阅读
- ios - SwiftUI 出现数组致命错误:嵌套 ForEach 和视图的索引超出范围
- c++ - 编译的 QT 程序在不调试时崩溃
- javascript - 使用 ExpressJS,如何使用 URL 从 SQLite 表中提取不同的数据?
- xamarin.forms - Xamarin.Forms CollectionView 所选项目有奇怪的灰色边框?
- java - jpackage 将文件添加到 installdir
- javascript - 将字符串转换为日期时间 Javascript
- javascript - 使用 react-native、Firebase 和 expo 的 signinwithemail 链接错误
- ssl - 使用通配符出口与自定义 CA 进行 TLS 握手
- python - 当用户更改文本字段时,ipyvuetify 捕获文本字段的用户输入值
- haskell - 如何快速检查任意函数