我试图将维基百科关于美国最高法院大法官的数据加载到R:
library(rvest)
html = html("http://en.wikipedia.org/wiki/List_of_Justices_of_the_Supreme_Court_of_the_United_States")
judges = html_table(html_nodes(html, "table")[[2]])
head(judges[,2])
[1] "Wilson, JamesJames Wilson" "Jay, JohnJohn Jay†"
[3] "Cushing, WilliamWilliam Cushing" "Blair, JohnJohn Blair, Jr."
[5] "Rutledge, JohnJohn Rutledge" "Iredell, JamesJames Iredell"
Run Code Online (Sandbox Code Playgroud)
问题是数据格式不正确.它不是出现在实际HTML表格中看到的名称("詹姆斯威尔逊"),而是实际出现两次,一次是"姓氏,名字",然后又是"名字姓氏".
原因是每个实际上都包含一个不可见的:
<td style="text-align:left;" class="">
<span style="display:none" class="">Wilson, James</span>
<a href="/wiki/James_Wilson" title="James Wilson">James Wilson</a>
</td>
Run Code Online (Sandbox Code Playgroud)
具有数字数据的列也是如此.我猜这个额外的代码是排序HTML表所必需的.但是,我不清楚在尝试从R中的表创建data.frame时如何删除这些跨度.
由于在R中很容易,我使用rvest包来解析HTML以从网站中提取信息.
我想知道在请求期间我的用户代理是什么(如果有的话),因为用户代理被分配到互联网浏览器或有办法以某种方式设置它?
我在HTML中打开会话和提取信息的代码如下:
library(rvest)
se <- html_session( "http://www.wp.pl" ) %>%
html_nodes("[data-st-area=Glonews-mozaika] li:nth-child(7) a") %>%
html_attr( name = "href" )
Run Code Online (Sandbox Code Playgroud) Rvest 选择选项,我认为用一个可重现的例子来解释是最容易的
网站:http : //www.verema.com/vinos/portada 我想获取葡萄酒的类型(Tipos de vinos),在 html 代码中是:
<select class="campo select" id="producto_tipo_producto_id" name="producto[tipo_producto_id]">
<option value="">Todos</option>
<option value="211">Tinto</option>
<option value="213">Blanco</option>
<option value="215">Rosado</option>
<option value="216">Espumoso</option>
<option value="217">Dulces y Generosos</option></select>
XPath : //*[@id="producto_tipo_producto_id"] or
CSS : #producto_tipo_producto_id or
Class: campo select
Run Code Online (Sandbox Code Playgroud)
我想要一个 data.frame 作为
211 丁托
213 布兰科
215 罗萨多
第216章
第217话
我的代码(R):
library(rvest)
Pagina.R <- html(x = "http://www.verema.com/vinos/portada")
text <- Pagina.R %>%
html_nodes(xpath='//*[@id="producto_tipo_producto_id"]')%>%
html_text()
text
values <- Pagina.R %>%
html_nodes(xpath='//*[@id="producto_tipo_producto_id"]')%>%
html_attr("option value") #problem????
values
Res <- data.frame(text = text, …Run Code Online (Sandbox Code Playgroud) 我想使用该rvest软件包从Pro Football Reference网站获取一些数据.首先,让我们从这个网址获取2015年所有游戏的结果http://www.pro-football-reference.com/years/2015/games.htm
library("rvest")
library("dplyr")
#grab table info
url <- "http://www.pro-football-reference.com/years/2015/games.htm"
urlHtml <- url %>% read_html()
dat <- urlHtml %>% html_table(header=TRUE) %>% .[[1]] %>% as_data_frame()
Run Code Online (Sandbox Code Playgroud)
这是你怎么做的?:)
dat可以清理一下.两个变量似乎都有名字的空白.此外,每周之间重复标题行.
colnames(dat) <- c("week", "day", "date", "winner", "at", "loser",
"box", "ptsW", "ptsL", "ydsW", "toW", "ydsL", "toL")
dat2 <- dat %>% filter(!(box == ""))
head(dat2)
Run Code Online (Sandbox Code Playgroud)
看起来不错!
现在让我们看一下个人游戏.在上面的网页上,点击表格第一行中的"Boxscore":9月10日在新英格兰和匹兹堡之间进行的比赛.这需要我们:http://www.pro-football-reference.com/boxscores/201509100nwe.htm.
我想抓住每个玩家的个人快照计数(大约在页面的一半).很确定这些是我们的前两行代码:
gameUrl <- "http://www.pro-football-reference.com/boxscores/201509100nwe.htm"
gameHtml <- gameUrl %>% read_html()
Run Code Online (Sandbox Code Playgroud)
但现在我无法弄清楚如何抓住我想要的特定桌子.我使用选择器小工具突出显示爱国者快照计数表.我通过在几个地方点击表格来执行此操作,然后"取消选择"突出显示的其他表格.我最终得到了一条道路:
#home_snap_counts .right , #home_snap_counts .left, #home_snap_counts .left, #home_snap_counts .tooltip, #home_snap_counts .left
每次尝试都会返回 …
我试图在需要登录的网站上抓一页,并且一直在收到403错误.
我已经为我的网站修改了这2个帖子的代码,使用rvest或httr登录网页上的非标准表单以及如何重用会话以避免在使用rvest进行重复登录时重复登录?
library(rvest)
pgsession <- html_session("https://www.optionslam.com/earnings/stocks/MSFT?page=-1")
pgform <- html_form(pgsession)[[1]]
filled_form <- set_values(pgform, 'username'='user', 'password'='pass')
s <- submit_form(pgsession, filled_form) # s is your logged in session
Run Code Online (Sandbox Code Playgroud)
代码运行时,我收到以下消息:
Submitting with 'NULL'
Warning message:
In request_POST(session, url = url, body = request$values, encode = request$encode, :
Forbidden (HTTP 403).
Run Code Online (Sandbox Code Playgroud)
我也通过这种方式运行代码,通过在注释中将user_agent更新为RS建议,但是,我收到与上面相同的错误.
library(rvest)
library(httr)
uastring <- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
pgsession <- html_session("https://www.optionslam.com/earnings/stocks/MSFT?page=-1", user_agent(uastring))
pgform <- html_form(pgsession)[[1]]
filled_form <- set_values(pgform, 'username'='user', 'password'='pass')
s …Run Code Online (Sandbox Code Playgroud) 我对用 R 抓取有点陌生,但我收到一条我无法理解的错误消息。我的代码:
url <- "https://en.wikipedia.org/wiki/California_State_Legislature,_2017%E2%80%9318_session"
leg <- read_html(url)
testdata <- leg %>%
html_nodes('table') %>%
.[6] %>%
html_table()
Run Code Online (Sandbox Code Playgroud)
我得到的回应是:
out[j + k, ] 中的错误:下标越界
当我用 html_text 换出 html_table 时,我没有收到错误消息。知道我做错了什么吗?
谢谢!
我想做什么xml2::xml_text()或rvest::html_text()做什么,但保留标签而不是<br>用\n. 目标是例如抓取网页,提取我想要的节点,并将纯 HTML 存储在变量中,就像write_html()将其存储在文件中一样。
我怎样才能做到这一点?
我有一组 html 页面。我想提取属性“border”= 1的所有表节点。这是一个例子:
<table border="1" cellspacing="0" cellpadding="5">
<tbody><tr><td>
<table border="0" cellpadding="2" cellspacing="0">
<tbody><tr>
<td bgcolor="#ff9999"><strong><font size="+1">CASEID</font></strong></td>
</tr></tbody>
</table>
<tr><td>[tbody]
</table>
Run Code Online (Sandbox Code Playgroud)
在示例中,我想选择 border=1 的表节点,而不是 border = 0 的表。我正在使用html_nodes()fromrvest但不知道如何添加属性:
html_nodes(x, "table")
Run Code Online (Sandbox Code Playgroud) 类似于How to deal with single quote in xpath,我想转义单引号。不同之处在于我不能排除双引号也可能出现在目标字符串中的可能性。
目标:
使用 Xpath(在 R 中)同时转义双引号和单引号。目标元素应用作变量,而不是像现有答案之一那样进行硬编码。(它应该是一个变量,因为我事先不知道内容,它可能有单引号、双引号或两者都有)。
作品:
library(rvest)
library(magrittr)
html <- "<div>1</div><div>Father's son</div>"
target <- "Father's son"
html %>% xml2::read_html() %>% html_nodes(xpath = paste0("//*[contains(text(), \"", target,"\")]"))
{xml_nodeset (1)}
[1] <div>Father's son</div>
Run Code Online (Sandbox Code Playgroud)
不起作用:
html <- "<div>1</div><div>Fat\"her's son</div>"
target <- "Fat\"her's son"
html %>% xml2::read_html() %>% html_nodes(xpath = paste0("//*[contains(text(), \"", target,"\")]"))
{xml_nodeset (0)}
Warning message:
In xpath_search(x$node, x$doc, xpath = xpath, nsMap = ns, num_results = Inf) :
Invalid expression [1207]
Run Code Online (Sandbox Code Playgroud)
我可以尝试“翻译为 …
我想抓取(使用rvest)一个要求用户同意设置 cookie 的网站。如果我只是抓取页面,则 rvest 只会下载弹出窗口。这是代码:
library(rvest)
content <- read_html("https://karriere.nrw/stellenausschreibung/dba41541-8ed9-4449-8f79-da3cda0cc07c")
content %>% html_text()
Run Code Online (Sandbox Code Playgroud)
结果似乎是请求同意的弹出窗口的内容。
有没有办法忽略或接受弹出窗口或提前设置 cookie 以便我可以访问网站的正文?