我正在尝试使用 rvest 从网站上抓取 HTML 表格。唯一的问题是我试图抓取的表格没有<tr>标签,除了第一行。它看起来像这样:
<tr>
<td>6/21/2015 9:38 PM</td>
<td>5311 Lake Park</td>
<td>UCPD</td>
<td>African American</td>
<td>Male</td>
<td>Subject was causing a disturbance in the area.</td>
<td>Name checked; no further action</td>
<td>No</td>
</tr>
<td>6/21/2015 10:37 PM</td>
<td>5200 S Blackstone</td>
<td>UCPD</td>
<td>African American</td>
<td>Male</td>
<td>Subject was observed fighting in the McDonald's parking lot</td>
<td>Warned; released</td>
<td>No</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
等等。因此,使用以下代码,我只能将第一行放入我的数据框中:
library(rvest)
mydata <- html_session("https://incidentreports.uchicago.edu/incidentReportArchive.php?startDate=06/01/2015&endDate=06/21/2015") %>%
html_node("table") %>%
html_table(header = TRUE, fill=TRUE)
Run Code Online (Sandbox Code Playgroud)
我如何改变它以使 html_table 理解行是行,即使它们没有开始标记<tr>?或者有更好的方法来解决这个问题吗?
我正在尝试使用 rvest 包从 R 中受密码保护的网站中抓取数据。我的代码当前在循环的每次迭代中都会登录到网站,该循环将运行大约 15,000 次。这看起来效率很低,但我还没有找到解决方法,因为每次都跳转到不同的网址而不先登录,然后返回到网站的登录页面。我的代码的简化如下:
library(rvest)
url <- password protected website url within quotes
session <-html_session(url)
form <-html_form(session)[[1]]
filled_form <- set_values(form,
`username` = email within quotes,
`password` = password within quotes)
start_table <- submit_form(session, filled_form) %>%
jump_to(url from which to scrape first table within quotes) %>%
html_node("table.inlayTable") %>%
html_table()
data_table <- start_table
for(i in 1:nrow(data_ids))
{
current_table <- try(submit_form(session, filled_form) %>%
jump_to(paste(first part of url within quotes, data_ids[i, ], last part of url within quotes, sep="")) %>%
html_node("table.inlayTable") …Run Code Online (Sandbox Code Playgroud) 我试图从http://www.footballoutsiders.com/stats/snapcounts抓取数据,但我无法更改网站下拉框中的字段(“团队”、“周”、“位置”) ,和“年”)。我试图用 rvest 抓取与 team = "ALL", week = "1", pos = "All", and year = "2015" 相关的表格如下。
url <- "http://www.footballoutsiders.com/stats/snapcounts"
pgsession <- html_session(url)
pgform <-html_form(pgsession)[[3]]
filled_form <-set_values(pgform,
"team" = "ALL",
"week" = "1",
"pos" = "ALL",
"year" = "2015"
)
submit_form(session=pgsession,form=filled_form, POST=url)
y <- read_html("http://www.footballoutsiders.com/stats/snapcounts")
y <- y %>%
html_nodes("table") %>%
.[[2]] %>%
html_table(header=TRUE)
Run Code Online (Sandbox Code Playgroud)
此代码返回与下拉框中的默认变量相关联的表,即 team = "ALL"、week = "20"、pos = "QB" 和 year = "2015",这是一个仅包含 11 个观测值的数据框。如果它真的改变了字段,它会返回一个包含 1,695 个观测值的数据框。
当前通过将不同的字符串传递给html_session()方法来更改 user_agent 。
有没有办法在抓取网站时更改计时器上的 IP 地址?
我想从网站下面定期提取一个表格。
单击积木名称(BLOK 16 A, BLOK 16 B, BLOK 16 C, ...)时,价格表会发生变化。URL 不改变,页面通过触发改变
javascript:__doPostBack('ctl00$ContentPlaceHolder1$DataList2$ctl04$lnk_blok','')
Run Code Online (Sandbox Code Playgroud)
在搜索 google 和 starckoverflow 后,我尝试了 3 种方法。
我尝试过的没有 1:这不会触发 doPostBack 事件。
postForm( "http://www.kentkonut.com.tr/tr/modul/projeler/daire_fiyatlari.aspx?id=44", ctl00_ContentPlaceHolder1_DataList2_ctl03_lnk_blok="ctl00$ContentPlaceHolder1$DataList2$ctl03$lnk_blok")
Run Code Online (Sandbox Code Playgroud)
我尝试过的没有 2: selenium remote 似乎可以在 ( http://localhost:4444/ ) 上工作,但 remotedriver 无法导航。返回此错误。(checkError(res) 中的错误:httr 调用中的未定义错误。httr 输出:length(url) == 1 不是 TRUE)
library(RSelenium)
startServer()
remDr <- remoteDriver()
remDr <- remoteDriver(remoteServerAddr = "localhost"
, port = 4444L, browserName = "firefox")
remDr$open()
remDr$getStatus()
remDr$navigate("http://www.kentkonut.com.tr/tr/modul/projeler/daire_fiyatlari.aspx?id=44")
Run Code Online (Sandbox Code Playgroud)
我尝试过的没有 3:这是触发 dopostback 事件的另一种方式。它不导航。
base.url <- "http://www.kentkonut.com.tr/tr/modul/projeler/",
event.target <- 'ctl00$ContentPlaceHolder1$DataList2$ctl03$lnk_blok',
action <- "daire_fiyatlari.aspx?id=44" …Run Code Online (Sandbox Code Playgroud) 我正在尝试抓取这个网站。不幸的是,我想使用 rvest 抓取的数据隐藏在按钮(加号)后面。
我试着用 rvest 包来做,我使用以下代码:
library(rvest)
url <- 'https://transparency.entsoe.eu/generation/r2/actualGenerationPerGenerationUnit/show?name=&defaultValue=true&viewType=TABLE&areaType=BZN&atch=false&dateTime.dateTime=17.03.2017+00:00|UTC|DAYTIMERANGE&dateTime.endDateTime=17.03.2017+00:00|UTC|DAYTIMERANGE&area.values=CTY|10YBE----------2!BZN|10YBE----------2&productionType.values=B02&productionType.values=B03&productionType.values=B04&productionType.values=B05&productionType.values=B06&productionType.values=B07&productionType.values=B08&productionType.values=B09&productionType.values=B10&productionType.values=B11&productionType.values=B12&productionType.values=B13&productionType.values=B14&productionType.values=B15&productionType.values=B16&productionType.values=B17&productionType.values=B18&productionType.values=B19&productionType.values=B20&dateTime.timezone=UTC&dateTime.timezone_input=UTC&dv-datatable_length=100'
htmlpage <- html_session(url) %>%
read_html() %>%
html_nodes(".dv-value-cell") %>>%
html_table()
Run Code Online (Sandbox Code Playgroud)
“.dv-value-cell”是使用 SelectorGadget 从网站中提取的(在rvest 的一个小插曲中)。
但是,在我可以使用此代码之前,我仍然需要打开加号菜单。单击按钮之前,此子表中的数据不存在。因此,上面的代码将返回一个空值。
我使用了这个问题中描述的 Chrome 网络开发工具来监控当我点击按钮时会发生什么。根据该信息,我看到有对以下网址的请求(缩短为仅突出显示与原始网址的区别):
https://transparency.entsoe.eu/...&dateTime.timezone_input=UTC&dv-datatable-detail_22WAMERCO000010Y_22WAMERCO000008L_length=10&dv-datatable_length=50&detailId=22WAMERCO000010Y_22WAMERCO000008L
Run Code Online (Sandbox Code Playgroud)
如您所见,这是原始 url,但还有一个小的额外请求。但是,当我在浏览器中尝试此 url 时,它没有显示所需的结果。我一定错过了网站另外通过的东西。
根据 Chrome,此请求的结果正是我要查找的数据(右键单击 > 复制 > 复制结果)。所以应该有一种方法可以下载这个特定的数据。
我也发现了这个关于类似问题的问题,但不幸的是,该解决方案非常适合这种情况,并且没有给出一般性解释。
如何重现此浏览器请求,以便我收到相同的表?
我想用来html_nodes从谷歌搜索结果中抓取组织的名称(我只需要第一个元素,假设这将是最好的猜测)。现在,我正在尝试使用其 xpath 定位第一个结果,并将其传递给 function html_nodes。为了找到 xpath,我使用 google chrome,如下图所示

这给了我//*[@id="rso"]/div[1]/div/div[1]/div/div/h3/a第一个结果标题的 xpath。但是,当我尝试将其传递给我时,html_nodes()我得到一个空字符串:
page %>% html_nodes(xpath='//*[@id="rso"]/div[1]/div/div[1]/div/div/h3/a')
{xml_nodeset (0)}
Run Code Online (Sandbox Code Playgroud)
虽然我期望字符串The A-Test 2017 Workshop.
如何a使用 xpath 或 css 获取该标签的内容?
我正在从一个网站上抓取数据,如下所示:
\n\n<div class="content">\n <blockquote>\n <div>\n Do not select this.\n </div>\n How do I select only this\xe2\x80\xa6\n <br />\n and this\xe2\x80\xa6\n <br />\n and this in a single node?\n </blockquote>\n</div>\nRun Code Online (Sandbox Code Playgroud)\n\n假设这样的代码片段在单个页面上出现 20 次,我想获取 中的所有文本,<blockquote>但忽略子节点(例如内部 )中的所有内容div。
因此我使用:
\n\nhtml %>%\n html_nodes(xpath = "//*[@class=\'content\']/blockquote/text()[normalize-space()]")\nRun Code Online (Sandbox Code Playgroud)\n\n然而,这将How do I select only this\xe2\x80\xa6, and this\xe2\x80\xa6,and this in a single node?分成结构内的各个元素xml_nodeset。
我应该怎么做才能将所有这些文本节点本质上连接成一个并返回相同的 20 个元素(或者返回一个元素,以防我所拥有的只是这个示例)?
\n我的问题与此类似,但后者没有收到我可以使用的答案。我正在使用 抓取数千个网址xml2::read_html。这很好用。purrr::map_df但是当我尝试使用and解析生成的 html 文档时html_nodes,出现以下错误:
Error in doc_namespaces(doc) : external pointer is not valid
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我无法使用示例重现该错误。下面的例子并不好,因为它工作得很好。但是,如果有人可以从概念上向我解释错误的含义以及如何解决它,那就太好了(这里有一个关于类似问题的github 线程,但我不遵循所有技术细节)。
library(rvest)
library(purrr)
urls_test <- list("https://en.wikipedia.org/wiki/FC_Barcelona",
"https://en.wikipedia.org/wiki/Rome")
h <- urls_test %>% map(~{
Sys.sleep(sample(seq(1, 3, by=0.001), 1))
read_html(.x)})
out <- h %>% map_df(~{
a <- html_nodes(., "#firstHeading") %>% html_text()
a <- if (length(a) == 0) NA else a
b <- html_nodes(., ".toctext") %>% html_text()
b <- if (length(b) == 0) NA else b
df <- tibble(a, b)
})
Run Code Online (Sandbox Code Playgroud)
会议信息: …
我有一些经验,但没有网站编码经验,并且认为我无法选择正确的 CSS 节点进行解析(我相信)。
library(rvest)
library(xml2)
library(selectr)
library(stringr)
library(jsonlite)
url <-'https://scholar.google.com/scholar?hl=en&as_sdt=0%2C38&q=apex+predator+conservation&btnG=&oq=apex+predator+c'
webpage <- read_html(url)
title_html <- html_nodes(webpage, 'a#rh06x-YUUvEJ')
title <- html_text(title_html)
head(title)
Run Code Online (Sandbox Code Playgroud)
最终,如果我可以将所有学者成果抓取并分成一个 csv 文件,其中包含“标题”、“作者”、“年份”、“期刊”等标题,那就太好了。任何帮助将非常感激!谢谢