r - How to write NA for missing results in rvest if there was no content in node (within loop) further how to merge variable with results
问题描述
Hi i'm new to R and try to fetch the tickers/symbols of Yahoo Finance from a text file which contains company names like Adidas, BMW etc. in order to run an event study later. This file contains about 800 names. Some of them can be found in yahoo and some not. (Thats ok)
My loop work so far but missing results won't be displayed. Further it only creates a table with numbers and results which could be found.But i would like to create a list which displayed the variable i ("firmen") and the results that's has been found or an NA in case there was no result.
Hope you guys can help me. Thank you !!!
my code:
library(rvest)
# company_names
firmen <- c(read.table("Mappe1.txt"))
# init
df <- NULL
# loop for search names in Yahoo Ticker Lookup
for(i in firmen){
# find url
url <- paste0("https://finance.yahoo.com/lookup/all?s=", i, "/")
page <- read_html(url,as="text")
# grab table
table <- page %>%
html_nodes(xpath = "//*[@id='lookup-page']/section/div/div/div/div[1]/table/tbody/tr[1]/td[1]") %>%
html_text() %>%
as.data.frame()
# bind to dataframe
df <- rbind(df, table)
}
解决方案
我解决了第一个问题,现在空节点(如果在 yahoo 页面上没有找到“i”)将显示为“NA”
这是代码:
library(rvest)
# teams
firmen <- c(read.table("Mappe1.txt"))
# init
df <- NULL
table <- NULL
# loop
for(i in firmen){
# find url
url <- paste0("https://finance.yahoo.com/lookup/all?s=", i, "/")
page <- read_html(url,as="text")
# grab ticker from yahoo finance
table <- page %>%
html_nodes(xpath = "//*[@id='lookup-page']/section/div/div/div/div[1]/table/tbody/tr[1]/td[1]") %>%
html_text(trim=TRUE) %>% replace(!nzchar(table), NA) %>%
as.data.frame()
# bind to dataframe
df <- rbind(df,table)
}
现在只剩下一个问题
如何将“df”和“firmen”合并到一个包含以下列的表中:
“股票代码” = df 和“坚定” = 坚定
因为 df 只有一列名为“。” 结果和列表firmen 包含许多公司,它们被放置在许多列中,但只有一行。
基本上我需要转换列表“坚定”,但我不知道如何
感谢您的帮助
推荐阅读
- python - 尝试在 python 3.7 中导入 ssl 时出现导入错误
- html - 从 Python 中的分页 URL 获取上一页和下一页表
- python - 为重复项添加增量值
- javascript - 如何在 REST API 查询上添加过滤器以查看来自 QnAmaker 的答案?
- javascript - 多个 Highchart React 图的实时更新
- reactjs - 映射到初始未定义值
- iis - 更改 IP 后无法访问 IIS 站点
- reactjs - 带有material-ui的条件滑块
- image - 过滤低对比度的噪声图像
- flutter - 在 Flutter 中隐藏原生/机器人按钮