gpt4 book ai didi

javascript - 从 R 中的 finviz 中抓取表格

转载 作者:行者123 更新时间:2023-12-05 04:41:30 28 4
gpt4 key购买 nike

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

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

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

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

最佳答案

这是一个使用 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…

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

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 是为整个 session 设置的,即您可以在不重新运行循环的情况下导航到多个 url 并从中抓取。

关于javascript - 从 R 中的 finviz 中抓取表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70053950/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com