我目前正在做一个项目,使用R来处理网页抓取。这是非常基本的,但我正在尝试了解它是如何工作的。
我使用谷歌股票作为我的网址,我使用谷歌股票行情自动收录器作为我正在查看的股票。
以下是我的代码:
# Declaring our URL variable
google = html("https://www.google.com/searchq=google+stock%5D&oq=google+stock%5D&aqs=chrome..69i57j0l2j69i60l3.5208j0j4&sourceid=chrome&ie=UTF-8")
# Prints and initializes the data
google_stock = google %>%
html_nodes("._FOc , .fac-l") %>%
html_text()
# Creating a data frame table
goggledf = data.frame(table(google_stock))
# Orders the data into highest frequency shown
googledf_order = googledf[order(-googledf$Freq),]
# Displays first few rows of data
head(googledf_order)
当我运行它时,我得到整数(0),
它应该显示股票价格。我不确定为什么这没有显示正确的股票价格。我还尝试运行代码直到 html_text(),
但它仍然没有向我显示我想要或需要的数据。
我只需要这个来显示网络上的股票价格。
我正在使用SelectorGadget
获取我的html节点(“.FOc,.fac-l”)
我认为你的URL可能有问题。当我试图将其粘贴到浏览器中时,我收到一个404错误。
与其抓取,不如使用 quantmod
包。要获取历史数据,您可以使用以下内容:
library(quantmod)
start <- as.Date("2018-01-01")
end <- as.Date("2018-01-20")
getSymbols("GOOGL", src = "google", from = start, to = end)
要获得当前的股票报价,您可以使用:
getQuote("GOOGL", src = "yahoo")
在quantmod
文档中,getQuote
函数“仅处理来自雅虎财务的采购报价”