css - 无法抓取 html 表格 rvest
问题描述
尝试使用 rvest 将https://www.cefconnect.com/closed-end-funds-daily-pricing 抓取到 R 数据框中。继续尝试各种 css 和 xpath 选择器,但无法收集主数据表。
使用了 'table'、'tbody'、'td' 和 'tr xpath 选择器但没有成功。两列是超链接,其余是静态文本。
URL <- 'https://www.cefconnect.com/closed-end-funds-daily-pricing'
html <- read_html(URL)
html2 <- html %>%
html_nodes(xpath = '//table') %>%
html_nodes(xpath = '//td/a[1]/text()') %>%
html_text()
html3 <- as.data.frame(html2)
解决方案
尝试分析在浏览器中加载网站时处理的请求。这可以通过在 Chrome 浏览器中浏览以下内容来完成。
设置 >> 更多工具 >> 开发者工具(快捷键:Ctrl + Shift + I)
如果您单击上面的链接,您将看到以 JSON 格式显示在表格中的数据。这是您需要转换为数据框的内容。
url<-"https://www.cefconnect.com/api/v3/DailyPricing?props=Ticker,Name,DistributionRateNAV,LastUpdated,Discount,DistributionRatePrice,ReturnOnNAV,CategoryId,CategoryName,IsManagedDistribution,Price,PriceChange,NAV,NAVPublished,Cusip/&_=1546832481302"
library(rvest)
page<-html_session(url)
json<-readBin(page$response$content, what="json")
library(jsonlite)
df<-fromJSON(json)
推荐阅读
- tfs - SonarQube 7.1 和 TFS 2018 发布质量门结果失败
- c# - 无论开始时间如何,将任务安排在每小时的第 35 分钟
- python - 使用 OpenCV 进行人脸识别中的背景对象检测
- spring - 如何针对接口中的注释参数编写测试?
- aws-lambda - AWS AppSync 在解析子资源之前等待 DynamoDB Streams Lambda 函数
- amazon-web-services - 具有上下文标识的 aws lambda 测试
- django - 如何在 Django 视图中注册期间输入电子邮件数据
- python-3.x - 根据日期时间索引中的条件删除数据框行
- tfs - 有没有办法为 TFS 或 VSTS 中的代理池保留管道?
- python - 将图层与输入相结合