Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webscrape标题和列表到带有rvest的数据帧_R_Dplyr_Rvest_Purrr - Fatal编程技术网

Webscrape标题和列表到带有rvest的数据帧

Webscrape标题和列表到带有rvest的数据帧,r,dplyr,rvest,purrr,R,Dplyr,Rvest,Purrr,我想将超链接刮到一个数据框中,其中的列如下所示。源页面包含标题和链接列表 题目(问题) hyperlink.title(确定) 超链接(OK) 获取链接和标题很简单(html\u节点“li”和“a”)。我不清楚如何将主题标题合并到最终的数据框架中 library(tidyverse) library(rvest) my.url <- read_html("http://www.secnav.navy.mil/fmc/fmb/Pages/Fiscal-Year-2019.aspx")

我想将超链接刮到一个数据框中,其中的列如下所示。源页面包含标题和链接列表

  • 题目(问题)
  • hyperlink.title(确定)
  • 超链接(OK)
获取链接和标题很简单(
html\u节点
“li”和“a”)。我不清楚如何将主题标题合并到最终的数据框架中

library(tidyverse)
library(rvest)

my.url <- read_html("http://www.secnav.navy.mil/fmc/fmb/Pages/Fiscal-Year-2019.aspx") %>% 
  html_nodes("#sharePointMainContent") 

hyperlink.title <- my.url %>% 
  html_nodes("li") %>% 
  html_text()

hyperlink <- my.url %>% 
  html_nodes("li") %>% 
  html_nodes("a") %>% 
  html_attr("href")

df <- tibble(title, hyperlink.title)
库(tidyverse)
图书馆(rvest)
my.url%
html_节点(“sharePointMainContent”)
超级链接.title%
html_节点(“li”)%>%
html_text()
超链接%
html_节点(“li”)%>%
html_节点(“a”)%>%
html_attr(“href”)
df%
html_text()%%>%str_trim()

由(v0.2.0)于2018-09-03创建。

该页面结构怪异,主表中有表格

我发现有效的方法是迭代(
map_df()
)父表的单元格(由
s4 wpcell plain
类标识)。每个单元格都包含另一个表,但我们可以简单地提取所需内容,而不必依赖
html\u table()

库(tidyverse)
图书馆(rvest)
#>正在加载所需的包:xml2
r%
html#U节点(“#sharePointMainContent>div>table”)%>%
html_节点(“.s4 wpcell plain”)%>%
地图(~{
标题%html\u节点('h3')%%>%html\u文本()%%>%str\u trim()
标题%html\u节点('li')%%>%html\u文本()
链接%html\u节点('a')%%>%html\u属性(“href”)
数据框(标题、标题、链接)
})
R
#>#A tibble:21 x 3
#>标题标题链接
#>                                                            
#>1海军部19财年摘要新闻简报http://www.secna…
#>2海军部摘要支持展品http://www.secna…
#>3海军部汇总预算概要手册http://www.secna…
#>4海军部总结底线http://www.secna…
#>5海军部向国会提交的关于…http://www.secna…
#>6海军部船舶建造计划概要…http://www.secna…
#>7军事人员计划军事人员,N…http://www.secna…
#>8军事人员计划军事人员、军事人员…http://www.secna…
#>9军事人员计划预备役人员,Na…http://www.secna…
#>10军事人员计划后备人员、军事人员和军事人员…http://www.secna…
#> # ... 还有11行

由(v0.2.0)于2018-09-04创建。

我认为它的格式可能很奇怪,但我是个新手,不太清楚。你的解决方案很有效!你的答案只会刮到这一页的左边。结果中缺少右侧以“采购”开头的列。可能是选择了
.s4 wpcell plain
,才选择了左侧。我相信你能找到一个更合适的选择器。
subject.heading <- my.url %>% 
  html_nodes("h3") %>% 
  html_text() %>% str_trim()