我目前正在处理一个使用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)当我运行这个程序时,我得到了integer(0),它应该显示一个股票价格。我不知道为什么这没有显示正确的股价。我还试着运行代码,直到html_text(),它仍然没有给我显示我想要或需要的数据。
我只需要这个来显示网络上的股价。
我使用SelectorGadget获取我的html节点("._FOc,..fac l“)
发布于 2018-01-21 18:05:27
我想你的网址可能有问题。当我试图将它粘贴到浏览器中时,我会得到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函数“只处理来自雅虎金融的报价”。
https://stackoverflow.com/questions/48362059
复制相似问题