我正在处理一个数据帧(称为full_df(,该数据帧包含我想用来刮取另外两个链接的链接。这是数据帧的示例:
structure(list(CIK = c("1082339", "1276755", "1280511"), COMPANY_NAME = c("COLDSTREAM CAPITAL MANAGEMENT INC",
"CHELSEA COUNSEL CO", "QUANTUM CAPITAL MANAGEMENT"), FORM_TYPE = c("13F-HR",
"13F-HR", "13F-HR"), FILE_DATE = c("2020-05-27", "2020-06-12",
"2020-05-26"), FORM_LINK = c("edgar/data/1082339/0001082339-20-000002.txt",
"edgar/data/1276755/0001420506-20-000683.txt", "edgar/data/1280511/0001280511-20-000003.txt"
), QTR_YEAR = c("Q22020", "Q22020", "Q22020"), FULL_LINK = c("https://www.sec.gov/Archives/edgar/data/1082339/0001082339-20-000002-index.htm",
"https://www.sec.gov/Archives/edgar/data/1276755/0001420506-20-000683-index.htm",
"https://www.sec.gov/Archives/edgar/data/1280511/0001280511-20-000003-index.htm"
)), row.names = c(NA, 3L), class = "data.frame")
我想迭代FULL_LINK列,并获得另外两个链接,然后我想将它们作为两个新列添加到我的原始数据帧中——xml_LINK和html_LINK。
我可以使用我这样写的函数来获得链接(这里用了一个例子(:
library(polite)
library(rvest)
library(glue)
library(tidyverse)
test_link <- "https://www.sec.gov/Archives/edgar/data/1082339/0001082339-20-000002-index.htm"
ua = 'Kartik P (for personal use)'
session <- bow("https://www.sec.gov/",
user_agent = ua)
xml_scraper <- function(urll) {
print(glue("Scraping: {urll}"))
temp_link <- session %>%
nod(urll) %>%
scrape(verbose = FALSE) %>%
html_nodes("a") %>%
html_attr('href')
xml_link <- temp_link %>%
nth(12)
html_link <- temp_link %>%
nth(11)
return(data.frame(xml_link, html_link))
}
太棒了!它按预期工作,并返回一个包含两列的数据帧,我想要
xml_scraper(test_link)
Scraping: https://www.sec.gov/Archives/edgar/data/1082339/0001082339-20-000002-index.htm
xml_link
1 /Archives/edgar/data/1082339/000108233920000002/CCMI13F2020Q1.xml
html_link
1 /Archives/edgar/data/1082339/000108233920000002/xslForm13F_X01/CCMI13F2020Q1.xml
但是,我想做的是迭代FULL_df中FULL_LINK列的每个元素,并将这两个新链接添加为原始数据帧中新创建的xml_LINK和html_LINK列的元素。感觉这应该可以通过purr::map_dfr和bind_cols调用或同时更改两个名称变量来实现,但我无法理解语法。
如果有任何关于如何与dplyr和purrr合作的建议,我们将不胜感激。
提前谢谢。
可能:
df_new <- bind_cols(map_dfr(df$FULL_LINK, xml_scraper), df)
结果:
#> # A tibble: 3 × 9
#> xml_link html_link CIK COMPANY_NAME FORM_TYPE FILE_DATE FORM_LINK QTR_YEAR
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 /Archive… /Archives… 1082… COLDSTREAM … 13F-HR 2020-05-… edgar/da… Q22020
#> 2 /Archive… /Archives… 1276… CHELSEA COU… 13F-HR 2020-06-… edgar/da… Q22020
#> 3 /Archive… /Archives… 1280… QUANTUM CAP… 13F-HR 2020-05-… edgar/da… Q22020
#> # … with 1 more variable: FULL_LINK <chr>
创建于2022-01-01由reprex包(v2.0.1(
您可以使用xml_scraper函数更改数据集。你需要做变异";"按行";,因为你的函数没有矢量化。
data_full<-data %>%
rowwise() %>%
mutate(xml_link=xml_scraper(FULL_LINK) %>% pluck("xml_link"),
html_link=xml_scraper(FULL_LINK) %>% pluck("html_link"))
#If you want just the results of the scrape, you can use map
the_xml<-data %>%
split(1:nrow(.)) %>%
map(~pluck(.x$"FULL_LINK")) %>%
map(xml_scraper) %>%
bind_rows()
您可以编辑函数以输出FULL_LINK,并使用它将2个新列连接到原始数据
xml_scraper <- function(urll) {
print(glue("Scraping: {urll}"))
temp_link <- session %>%
nod(urll) %>%
scrape(verbose = FALSE) %>%
html_nodes("a") %>%
html_attr('href')
xml_link <- temp_link %>%
nth(12)
html_link <- temp_link %>%
nth(11)
return(data.frame(FULL_LINK = urll, xml_link, html_link))
}
然后
data2 <- map_dfr(data$FULL_LINK, .f = xml_scrapper) %>%
left_join(data, ., by = "FULL_LINK")