【问题标题】:Scraping tables from finviz in R从 R 中的 finviz 抓取表
【发布时间】:2021-11-21 11:09:08
【问题描述】:

我想从www.finviz.com 中提取季度表:损益表、资产负债表和现金流。我对多只股票感兴趣,但要使其自动化,我必须知道如何刮取一只股票。一个例子如下:

https://finviz.com/quote.ashx?t=A&ty=c&p=d&b=1

我们可以在页面底部看到这些表格。在此页面上,我们可以使用 rvest 抓取其他表格,但这是不同的情况,我无法抓取上述表格。

如果有人能帮助我解决这个问题,我将不胜感激。

【问题讨论】:

    标签: javascript r web-scraping


    【解决方案1】:

    这是一个使用rvestRSelenium 的示例。请注意,rsDriver 部分可能有些棘手:需要安装兼容的浏览器(对我来说是 Firefox),并且不需要占用 port

    代码

    library(rvest)
    library(tidyverse)
    library(RSelenium)
    
    url <- "https://finviz.com/quote.ashx?t=A&ty=c&p=d&b=1"
    
    # setup Selenium
    # (this will start a firefox window; may be necessary to
    # accept privacy preferences or similar manually!)
    rD <- rsDriver(port = 4450L, browser = "firefox")
    remDr <- rD$client
    
    # navigate to the url
    remDr$navigate(url)
    
    # define selector for buttons in cookie preferences overlay
    b_read_selector <- ".lcqSKB"
    # find button and click (twice)
    for(i in 1:2) {
      b_read <- remDr$findElements("css selector", b_read_selector)[[1]]
      b_read$clickElement()
      Sys.sleep(1)
    }
    
    # define selectors for the quarterly link and the table
    q_link_selector <- "#statements > table.fullview-links > tbody > tr > td:nth-child(2) > a:nth-child(2)"
    tab <- "#statements > .snapshot-table2"
    
    # find the 'quarterly' link and click
    q_link <- remDr$findElements("css selector", q_link_selector)[[1]]
    q_link$clickElement()
    
    # get the page source and parse html
    pg <- remDr$getPageSource()[[1]] %>% read_html()
    
    # then parse the table to tibble
    pg %>% html_node(tab) %>% html_table(header = T) %>% select(-2)
    
    # close the firefox session
    rD$server$stop()
    

    请注意,我将第二列(带有条形图的那一列)放在倒数第二行,因为图像没有被刮掉,所以该列无论如何都是空的。

    结果

    # A tibble: 20 × 9
       `Period End Date` `7/31/2021` `4/30/2021` `1/31/2021` `10/31/2020`  `7/31/2020`  `4/30/2020`  `1/31/2020` 
       <chr>             <chr>       <chr>       <chr>       <chr>         <chr>        <chr>        <chr>       
     1 Period Length     "3 Months"  "3 Months"  "3 Months"  3 Months      3 Months     3 Months     3 Months    
     2 Total Revenue     "1,586.00"  "1,525.00"  "1,548.00"  Upgrade your… Upgrade you… Upgrade you… Upgrade you…
     3 Cost of Revenue   "734.00"    "708.00"    "710.00"    Upgrade your… Upgrade you… Upgrade you… Upgrade you…
     4 Gross Profit      "852.00"    "817.00"    "838.00"    Upgrade your… Upgrade you… Upgrade you… Upgrade you…
    

    要获取资产负债表或现金流数据,您需要在解析页面源之前单击相应的链接,该链接的工作方式与季度链接类似:

    balance_link_selector <- "#statements > table.fullview-links > tbody > tr > td:nth-child(1) > a:nth-child(2)"
    cash_link_selector <- "#statements > table.fullview-links > tbody > tr > td:nth-child(1) > a:nth-child(3)"
    
    # e.g. to get balance sheet:
    balance_link <- remDr$findElements("css selector", balance_link_selector)[[1]]
    
    # click balance sheet link
    balance_link$clickElement()
    
    ... then proceed as above.
    
    

    这应该使您能够编写使流程自动化的函数/循环。请注意,在第一次调用 remDr$navigate(url) 之后运行代码以关闭 cookie 首选项覆盖 一次 就足够了,因为 cookie 是为整个会话设置的,即您可以导航到多个会话并从中抓取之后的 url,无需重新运行循环。

    【讨论】:

    • 非常感谢您的帮助,马丁。我收到以下错误:> pg %>% html_node(tab) %>% html_table(header = T) %>% select(-2) 错误:表的列数不一致。你想要fill = TRUE吗? > pg %>% html_node(tab) %>% html_table(header = T, fill = T) %>% select(-2) 输出错误 [j + k, ] : 下标越界
    • 我只是重新运行了代码,它运行完美。似乎get_table()解析的表格布局会引起麻烦。 url 的代码是否像我的回答(和你的问题)一样运行?
    • 我已经重新运行了代码,它可以运行到“pg%>% html_node (tab)”。 “html_table”函数失败,输出是引用的错误。我不知道网络上接受 cookie 的消息是否会导致问题。感谢您对此错误的帮助。
    • 您收到的错误是指已弃用的功能。更新可能会解决这个问题(packageVersion("rvest") 产生了什么?我有 rvest 1.0.1)。
    • rvest 0.3.6。我已经更新到 1.0.2。现在,代码的第一部分有效,但由于接受 cookie 的消息,我得到了“年度损益表”,而不是“季度损益表”。这就是为什么 "q_link$clickElement()" "balance_link$clickElement()" 和 "cash_link$clickElement()" 不起作用并且输出是相同的:“年度损益表”。有没有办法解决这个问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多