正如标题所述,我很好奇如果无法在特定页面上找到属性,那么包中的html_text()函数是否可以rvest存储NA值.
我目前正在运行超过199页(其工作正常;已经在一些变量上测试).
目前,当我搜索仅存在于199页中的某些(136)中的值时,html_text()仅返回136个字符串的向量.这没用,因为没有NAs我无法确定哪些页面包含有问题的变量.
我看到它html_atts()能够接收default输入,但不能html_text().有小费吗?
非常感谢!
我试图刮取页面https://en.wikipedia.org/wiki/UEFA_Euro_2012_squads并可以使用rvest取消文本数据
library(plyr)
library(XML)
library(rvest)
library(dplyr)
library(magrittr)
library(data.table)
for(i in 1:16)
{
float <- paste("squad", i, sep ="")
print(float)
html = read_html("https://en.wikipedia.org/wiki/UEFA_Euro_2012_squads")
assign(float, html_table(html_nodes(html, "table")[[i]]))
}
Run Code Online (Sandbox Code Playgroud)
但是也想在俱乐部的每张桌子上添加一个额外的栏目.例如对于小队1(页面上的波兰小队,截断以显示前5名球员)
0#0 Pos. Player Date of birth (age) Caps Goals Club
1 1 1GK Wojciech Szczęsny (1990-04-18)18 April 1990 (aged 22) 11 0 Arsenal
2 2 2DF Sebastian Boenisch (1987-02-01)1 February 1987 (aged 25) 9 0 Werder Bremen
3 3 2DF Grzegorz Wojtkowiak (1984-01-26)26 January 1984 (aged 28) 19 0 Lech Poznań
4 …Run Code Online (Sandbox Code Playgroud) 我有一系列 9 个网址,我想从中抓取数据:
http://www.basketball-reference.com/play-index/draft_finder.cgi?request=1&year_min=2001&year_max=2014&round_min=&round_max=&pick_overall_min=&pick_overall_max=&franch_id=&college_id=0&is_active=&is_hof=&pos_is_g=Y&pos_is_gf=Y&pos_is_f=Y&pos_is_fg=Y&pos_is_fc=Y&pos_is_c=Y&pos_is_cf=Y&c1stat=&c1comp=&c1val=&c2stat=&c2comp=&c2val=&c3stat=&c3comp=&c3val=&c4stat=&c4comp=&c4val=&order_by=year_id&order_by_asc=&offset=0
Run Code Online (Sandbox Code Playgroud)
当页面更改到最后一页时,链接末尾的 offset= 从 0 到 900(乘以 100)。我想遍历每个页面并抓取每个表,然后使用 rbind 将每个 df 按顺序堆叠在一起。我一直在使用 rvest 并且想使用 lapply 因为我比 for 循环更好。
问题与此类似(从 url 列表中收获 (rvest) 多个 HTML 页面)但不同,因为我不想在运行程序之前将所有链接复制到一个向量。我想要一个关于如何遍历多个页面并收集数据的通用解决方案,每次创建一个数据框。
以下适用于第一页:
library(rvest)
library(stringr)
library(tidyr)
site <- 'http://www.basketball-reference.com/play-index/draft_finder.cgi?request=1&year_min=2001&year_max=2014&round_min=&round_max=&pick_overall_min=&pick_overall_max=&franch_id=&college_id=0&is_active=&is_hof=&pos_is_g=Y&pos_is_gf=Y&pos_is_f=Y&pos_is_fg=Y&pos_is_fc=Y&pos_is_c=Y&pos_is_cf=Y&c1stat=&c1comp=&c1val=&c2stat=&c2comp=&c2val=&c3stat=&c3comp=&c3val=&c4stat=&c4comp=&c4val=&order_by=year_id&order_by_asc=&offset=0'
webpage <- read_html(site)
draft_table <- html_nodes(webpage, 'table')
draft <- html_table(draft_table)[[1]]
Run Code Online (Sandbox Code Playgroud)
但我想在所有页面上重复这一点,而不必将 url 粘贴到向量中。我尝试了以下方法,但没有奏效:
jump <- seq(0, 900, by = 100)
site <- paste('http://www.basketball-reference.com/play-index/draft_finder.cgi?request=1&year_min=2001&year_max=2014&round_min=&round_max=&pick_overall_min=&pick_overall_max=&franch_id=&college_id=0&is_active=&is_hof=&pos_is_g=Y&pos_is_gf=Y&pos_is_f=Y&pos_is_fg=Y&pos_is_fc=Y&pos_is_c=Y&pos_is_cf=Y&c1stat=&c1comp=&c1val=&c2stat=&c2comp=&c2val=&c3stat=&c3comp=&c3val=&c4stat=&c4comp=&c4val=&order_by=year_id&order_by_asc=&offset=', jump,'.htm', sep="")
webpage <- read_html(site)
draft_table <- html_nodes(webpage, 'table')
draft <- html_table(draft_table)[[1]]
Run Code Online (Sandbox Code Playgroud)
所以每个页面都应该有一个数据框,我想把它们放在一个列表中然后使用 rbind 来堆叠它们会更容易。
任何帮助将不胜感激!
我试图刮掉从日常预测FiveThirtyEight使用rvest,但我感兴趣的对象似乎是一个JavaScript对象,我有困难甚至定位在哪里以及如何寻找。(我不精通 CSS 或 Javascript,尽管我在过去几天尝试自学。)
通过检查网页元素和 CSS 选择器,我发现了以下内容:
要查看的位置是<div id="polling-avg-chart">,所以我尝试了
library(rvest)
url <-
"https://projects.fivethirtyeight.com/election-2016/national-primary-polls/democratic/"
url %>%
read_html() %>%
html_nodes("#polling-avg-chart")
Run Code Online (Sandbox Code Playgroud)
没有太大的成功。输出很简单
{xml_nodeset (1)}
[1] <\div id="polling-avg-chart"></div>\n
以点为单位的单个投票结果在 中<g style="clip-path: url("#line-clippoll_avg");"> ... </g>,您可以在其中看到 502 个位置的数字。我猜,我将不得不转换cx和cy每个节点到相应的百分比,这是做的<g class="flag-box" transform="translate(30, 161.44093322753096)">...</g>等等。
但是,我没有看到预测线的基础数据,而不是点。
<line class="hover-date-line hide-line">变化之类的事物,以及诸如<path class="link" d="M 0 171.40106812500002 C 15 171.40106812500002 15 170.94093803735575 30 170.94093803735575"></path>变化之类的值,我猜测这些值是创建每日预测线的原因。我看过一些其他职位SO如这个,但他们都不似乎适用于这一特定问题。在整洁的数据框中获得预测百分比的最佳方法是什么?
我正在使用 R rvest 库来读取包含表格的 html 页面。不幸的是,这些表的列数不一致。
这是我阅读的表格示例:
<table>
<tr class="alt">
<td>1</td>
<td>2</td>
<td class="hidden">3</td>
</tr>
<tr class="tr0 close notule">
<td colspan="9">4</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
以及我在 R 中读取表格的代码:
require(rvest)
url = "table.html"
x <- read_html(url)
(x %>% html_nodes("table")) %>% html_table(fill=T)
# [[1]]
# X1 X2 X3 X4 X5 X6 X7 X8 X9
# 1 1 2 3 NA NA NA NA NA NA
# 2 4 4 4 4 4 4 4 4 4
Run Code Online (Sandbox Code Playgroud)
我想避免考虑隐藏类的 td 和类 'tr0 close notule' 的 tr,因此我只能得到如下表: …
早上好,
\n我是使用 R 进行抓取的新手,并且很难以有用的方式从网页中抓取元素列表。
\n这是我的脚本
\nlibrary(rvest)\n\nurl <- read_html("https://www.pole-emploi.fr/annuaire/provins-77070")\n\nwebpage <- url %>%\n html_nodes('.zone') %>%\n html_text()\nwebpage\n \nRun Code Online (Sandbox Code Playgroud)\n当我运行脚本时,所有元素都挤在一起,之间没有任何空格,这是可以理解的,因为每个项目都包含在一个单独的元素中
[1] "77114GouaixHerm\xc3\xa9Noyen-sur-SeineVilliers-sur-Seine" \n [2] "77118BalloyBazoches-l\xc3\xa8s-BrayGravon" \nRun Code Online (Sandbox Code Playgroud)\n我想让它们像这样(或用逗号分隔)
\n[1] "77114 Gouaix Herm\xc3\xa9 Noyen-sur-Seine Villiers-sur-Seine" \n[2] "77118 Balloy Bazoches-l\xc3\xa8s-Bray Gravon"\nRun Code Online (Sandbox Code Playgroud)\n或者格式整洁更好
\n Postal City\n 77114 Gouaix\n 77114 Herm\xc3\xa9\n 77114 Noyen-sur-Seine\n 77114 Villiers-sur-Seine\nRun Code Online (Sandbox Code Playgroud)\n我尝试在页面中找到其他选择器或 Xpath,但没有成功。我最多的就是选择列表中的一个元素。
\n任何帮助将不胜感激。
\n提前致谢。
\n我正在尝试使用 R 来抓取https://www.rotowire.com/football/player.php?id=4307上的各种表格,但是由于他们使用 javascript,我遇到了一些障碍。我已经安装了 rvest 和 V8 库并尝试找到正确的节点,但是我很确定我没有正确指定正确的表节点。我咨询了网站所有者,他们同意人们抓取他们的数据。
\n\nV8 网页包含一段用于抓取电子邮件地址的示例代码。我尝试修改该代码以适合我的目的。
\n\n#Loading both the required libraries\nlibrary(rvest)\nlibrary(V8)\n\nlink <- \'https://www.rotowire.com/football/player.php?id=4307\'\nemailjs <- read_html(link) %>% html_nodes(\'div\') %>% html_nodes(\'basicStats\') %>% html_text()\n\nct <- v8()\nread_html(ct$eval(gsub(\'document.write\',\'\',emailjs))) %>% \n html_text()\nRun Code Online (Sandbox Code Playgroud)\n\n没有成功
\n\n我也尝试过:
\n\nemailjs <- read_html(link) %>% html_nodes(\'div\') %>% html_nodes(\'script\') %>% html_text()\nct <- v8()\nread_html(ct$eval(gsub(\'document.write\',\'\',emailjs))) %>% \n html_text()\nRun Code Online (Sandbox Code Playgroud)\n\n也:
\n\nemailjs <- read_html(link) %>% html_nodes(\'div\') %>% html_nodes(\'basicStats\') %>% html_text()\nRun Code Online (Sandbox Code Playgroud)\n\n第一个代码块失败是因为我错误地指定了节点,或者至少我认为这是原因。
\n\n第二组代码会撤回所有内容,但会出现以下错误:
\n\nError in context_eval(join(src), private$context) : \n ReferenceError: window is not …Run Code Online (Sandbox Code Playgroud) 我希望在表格中添加副标题和小计/边距行。最终,我正在寻找如下所示的结构,我将使用openxlsx和将其导出到 Excel writeData。
| 2019年 | 2020年 | 2021年 | |
|---|---|---|---|
| A | |||
| A1 | 1001 | 第1157章 | 911 |
| A2 | 1005 | 803 | 1110 |
| A3 | 1125 | 第897章 | 1190 |
| 总A | 3131 | 2857 | 3211 |
| 乙 | |||
| B1 | 806 | 第982章 | 1098 |
| B2 | 1106 | 第945章 | 1080 |
| B3 | 1057 | 1123 | 第867章 |
| 总B | 2969 | 3050 | 3045 |
| C | |||
| C1 | 第847章 | 1087 | 1140 |
| C2 | 1146 | 966 | 第1176章 |
| C3 | 1071 | 915 | 第892章 |
| 总碳 | 3064 | 2968 | 3208 |
| 总计 全部 | 9164 | 8875 | 9464 |
我怀疑小标题和小计是完全不同的问题,但我在这里问这两个问题,以防有一个与每个问题相关的通用方法。
创建示例数据(长格式):
d <- data.frame(
year = rep(c(2019, 2020, 2021), times = 9), …Run Code Online (Sandbox Code Playgroud) 我正在使用 进行数据提取任务R。数据是在Power BI仪表板中分配的,所以获取起来非常麻烦。我在这里找到了解决方案:
但我不确定如何在页面中导航以获取组件并提取表格。我的代码如下:
library(wdman)
library(RSelenium)
library(xml2)
library(selectr)
library(tidyverse)
library(rvest)
# using wdman to start a selenium server
remDr <- rsDriver(
port = 4445L,
browser = "firefox"
)
#remDr$open()
remDr <- remoteDriver(port = 4445L,browser = "firefox")
# open a new Tab on Chrome
remDr$open()
# navigate to the site you wish to analyze
report_url <- "https://app.powerbi.com/view?r=eyJrIjoiOGI5Yzg2MGYtZmNkNy00ZjA5LTlhYTYtZTJjNjg2NTY2YTlmIiwidCI6ImI1NDE0YTdiLTcwYTYtNGUyYi05Yzc0LTM1Yjk0MDkyMjk3MCJ9"
remDr$navigate(report_url)
# fetch the data
data_table <- read_html(remDr$getPageSource()[[1]]) %>%
querySelector("div.pivotTable")
Run Code Online (Sandbox Code Playgroud)
虽然硒进程工作,但我不知道如何获取我的表:
蓝色箭头显示了我想要的表格,然后我需要移动到其他页面来提取剩余的表格。但我想如果第一页能做到的话,其他页面也会一样。
非常感谢!
I have been exploring the rvest package and have a question regarding extracting urls from a list. My goal is to generate a df with the following headers: Country, City and the URL for the city. I already have a df with each of the countries and a list with the cities for each country.
My question is, how can I reference each city so that I can obtain its respective URL link? I am trying to reference the href …