【问题标题】:R - getting no data when scrapingR - 抓取时没有数据
【发布时间】:2021-03-07 22:39:06
【问题描述】:

我遇到了与此处列出的相同的问题;但是,我使用的是 R 而不是 python:Getting no data when scraping a table

我正在尝试从 https://coinmarketcap.com/currencies/bitcoin/historical-data/ 抓取历史比特币数据

这是我正在运行的代码。 url_tables 产生一个 0 的列表。

library(tidyverse)
library(rvest)

url<-'https://coinmarketcap.com/currencies/bitcoin/historical-data/'

#extract html

url_html<-read_html(url)

#table extraction

url_tables<- url_html %>% html_table(fill=TRUE)

任何帮助将不胜感激。我有点菜鸟。

【问题讨论】:

  • 该表格是 javascript 动态生成内容的一部分,因此 rvest 可能不足以正确呈现它。一种替代方法是使用 Selenium。

标签: r web-scraping rvest


【解决方案1】:

这里我使用 Selenium 来控制 Firefox 浏览器。 请注意,您必须安装 Selenium 并将其添加到操作系统的 PATH 才能使其正常工作。详情见这里:https://www.selenium.dev/documentation/en/webdriver/driver_requirements/

library(tidyverse)
library(rvest)
library(RSelenium)
    
url<-'https://coinmarketcap.com/currencies/bitcoin/historical-data/'

rD <- rsDriver(browser = "firefox", port=4545L, verbose=TRUE) #If port 4525 does not work, simply change it to 4546, 4547...
remDr <- rD[["client"]]

remDr$navigate(url = url) #This should open a Firefox instance in a new window.
    
#capture html
obj_html<-remDr$getPageSource()[[1]] %>% read_html(encoding = "UTF-8")

#table extraction
table <- obj_html %>% html_nodes(xpath = "//div/table") %>% html_table(fill = TRUE) %>% as.data.frame() %>% as_tibble()


# A tibble: 58 x 7
   Date     Open.   High    Low     Close.. Volume    Market.Cap 
   <chr>    <chr>   <chr>   <chr>   <chr>   <chr>     <chr>      
 1 Mar 06,~ $48,89~ $49,14~ $47,25~ $48,91~ $34,363,~ $912,054,1~
 2 Mar 05,~ $48,52~ $49,39~ $46,54~ $48,92~ $48,625,~ $912,285,0~
 3 Mar 04,~ $50,52~ $51,73~ $47,65~ $48,56~ $52,343,~ $905,414,1~
 4 Mar 03,~ $48,41~ $52,53~ $48,27~ $50,53~ $53,220,~ $942,236,5~
 5 Mar 02,~ $49,61~ $50,12~ $47,22~ $48,37~ $47,530,~ $901,933,6~
 6 Mar 01,~ $45,15~ $49,78~ $45,11~ $49,63~ $53,891,~ $925,235,5~
 7 Feb 28,~ $46,19~ $46,71~ $43,24~ $45,13~ $53,443,~ $841,428,9~
 8 Feb 27,~ $46,34~ $48,25~ $45,26~ $46,18~ $45,910,~ $860,978,1~
 9 Feb 26,~ $47,18~ $48,37~ $44,45~ $46,33~ $350,967~ $863,752,2~
10 Feb 25,~ $49,70~ $51,94~ $47,09~ $47,09~ $54,506,~ $877,766,1~
# ... with 48 more rows

【讨论】:

  • 谢谢,这绝对看起来你有我想要的!我刚刚收到此错误消息:无法打开 chrome 浏览器。客户端错误消息:摘要:SessionNotCreatedException 详细信息:无法创建新会话。更多详细信息:运行 errorDetails 方法检查服务器日志以获取更多详细信息。
  • 它可能告诉你端口 4545 正在使用中。尝试将其更改为其他内容,例如 4546 或 4550。请参阅我在代码中的注释。例如:rD &lt;- rsDriver(browser = "firefox", port=4546L, verbose=TRUE)
  • 不,我有几次这个错误,所以改变了端口,直到我得到这个错误!下班后我会再玩一场。谢谢!
猜你喜欢
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 1970-01-01
  • 2018-12-01
  • 2018-04-05
  • 2021-02-16
相关资源
最近更新 更多