首页 > 解决方案 > 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)

}

标签: rloopsmergenarvest

解决方案


我解决了第一个问题,现在空节点(如果在 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 包含许多公司,它们被放置在许多列中,但只有一行。

基本上我需要转换列表“坚定”,但我不知道如何

感谢您的帮助


推荐阅读