Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto_history not working #49

Open
augur2 opened this issue Nov 4, 2020 · 16 comments
Open

crypto_history not working #49

augur2 opened this issue Nov 4, 2020 · 16 comments

Comments

@augur2
Copy link

augur2 commented Nov 4, 2020

The function crypto_history is not working anymore

I think the problem is, that coinmarketcap changed their website and now the scraper function inside the crypto_history function reads the wrong table on the website:

library(crypto)
crypto_history(c("ETH"), start_date = 20201005)

Error in names(x) <- value :
'names' attribute [11] must be the same length as the vector [6]

@augur2
Copy link
Author

augur2 commented Nov 5, 2020

wrote my own function:

scrape_coinmarketcap.txt

@jas1
Copy link

jas1 commented Nov 11, 2020

same issue:

same issue, slightly different funciton call:

library("crypto")
df_history <- crypto_history(limit=20)

Error in names(x) <- value :
'names' attribute [11] must be the same length as the vector [6]

@KyleBenzle
Copy link

Yep, came here for the same issue. Augur2 great function! Thank you!
How were you able to diagnose the issue of it reading the wrong table on coinmarket cap?

Can anyone suggest another package with similar functionality?

@augur2
Copy link
Author

augur2 commented Nov 12, 2020

Yep, came here for the same issue. Augur2 great function! Thank you!
How were you able to diagnose the issue of it reading the wrong table on coinmarket cap?

Can anyone suggest another package with similar functionality?

Thanks KyleBenzle!

I went to 'Inspect element' in my browser and searched for some numbers that I saw in the table...

Maybe this is an alternative / more elegant approach:

https://stackoverflow.com/questions/64761914/scraping-historical-data-from-coinmarketcap

@isaaczhao23
Copy link

isaaczhao23 commented Nov 15, 2020

wrote my own function:

scrape_coinmarketcap.txt

Thanks augur2! Noting for others that the input for currency is the symbol not the actual name anymore (ex: "Bitcoin" is now "BTC" using this function.

@augur2
Copy link
Author

augur2 commented Nov 18, 2020

updated the function:

scrape_coinmarketcap.txt

@deanfantazzini
Copy link

See also here for a similar solution ... and much more 👍

https://github.com/deanfantazzini/bitcoinFinance

@JesseVent
Copy link
Owner

Sorry guys been sidelined with work priorities and havent really given this package the time I should have..

This is pretty much the godsend for working out the classes you need to extract https://selectorgadget.com/...

I'll try have a look at whats happening

@JesseVent
Copy link
Owner

I just pushed a change that pretty much just aligns it to what you were doing @augur2 so thanks for that - please test it out by installing latest version devtools::install_github("jessevent/crypto")

Thanks

@augur2
Copy link
Author

augur2 commented Dec 29, 2020

Not working again...

Getting the following error:

histeth <- crypto_history(c("ETH"), start_date = 20201129)
♥ If this helps you become rich please consider donating

ERC-20: 0x375923Bf82F0b728d23A5704261a6e16341fd860
XRP: rK59semLsuJZEWftxBFhWuNE6uhznjz2bK

Scraping historical crypto data

Error in table$props$initialState$cryptocurrency$ohlcvHistorical[[1]] :
subscript out of bounds

seems like CMP changed their website again

@deanfantazzini
Copy link

deanfantazzini commented Dec 29, 2020

As I wrote on my Github page for the R package bitcoinFinance, unfortunately, it is clear that coinmarketcap wants to monetize their historical data and they are pushing people to subscribe to their commercial API. This is the third (!!!) change of their website for historical data in less than 1 year. Currently, I have no time to deal with it. If someone can find a solution, please post it here, or on the Github page of my package bitcoinFinance. Thanks!

@realauggieheschmeyer
Copy link

Hey all,

I was able to come up with a solution to the discussed problem. Long story short: I used coingecko.com instead of coinmarketcap.com. I'll try to open a PR to see if @JesseVent will incorporate my changes. In the meantime, feel free to use the code chunk below. You'll need the following packages to make it work: tidyvese, rvest, 'janitor, and timetk`. I'm working on making it scalable to more than a single coin and I'll update my code when I get it.

library(tidyverse)
library(rvest)

currency_list <- c("bitcoin")

create_url <- function(currency, start_date = Sys.Date() %-time% "1 year", end_date = Sys.Date()) {
  
  suppressPackageStartupMessages(library(timetk))
  
  page <- str_c(
    "https://www.coingecko.com/en/coins/",
    currency,
    "/historical_data/usd?",
    "end_date=",
    end_date,
    "&start_date=",
    start_date
  )
  
  return(page)
  
}

scrape_data <- function(page, currency) {
  
  data <- page %>% 
    read_html() %>% 
    html_node(xpath = '//table') %>% 
    html_table() %>% 
    as_tibble() %>% 
    janitor::clean_names() %>% 
    mutate(
      currency = currency,
      across(market_cap:close, parse_number)
    ) %>% 
    select(currency, date, close) %>% 
    drop_na()
  
  return(data)
  
}

currency_list %>% 
  map_chr(create_url) %>% 
  map_df(scrape_data, currency = "bitcoin")

@augur2
Copy link
Author

augur2 commented Jan 1, 2021

Hey all,

I was able to come up with a solution to the discussed problem. Long story short: I used coingecko.com instead of coinmarketcap.com. I'll try to open a PR to see if @JesseVent will incorporate my changes. In the meantime, feel free to use the code chunk below. You'll need the following packages to make it work: tidyvese, rvest, 'janitor, and timetk`. I'm working on making it scalable to more than a single coin and I'll update my code when I get it.

library(tidyverse)
library(rvest)

currency_list <- c("bitcoin")

create_url <- function(currency, start_date = Sys.Date() %-time% "1 year", end_date = Sys.Date()) {
  
  suppressPackageStartupMessages(library(timetk))
  
  page <- str_c(
    "https://www.coingecko.com/en/coins/",
    currency,
    "/historical_data/usd?",
    "end_date=",
    end_date,
    "&start_date=",
    start_date
  )
  
  return(page)
  
}

scrape_data <- function(page, currency) {
  
  data <- page %>% 
    read_html() %>% 
    html_node(xpath = '//table') %>% 
    html_table() %>% 
    as_tibble() %>% 
    janitor::clean_names() %>% 
    mutate(
      currency = currency,
      across(market_cap:close, parse_number)
    ) %>% 
    select(currency, date, close) %>% 
    drop_na()
  
  return(data)
  
}

currency_list %>% 
  map_chr(create_url) %>% 
  map_df(scrape_data, currency = "bitcoin")

Thanks realauggieheschmeyer.

To make your solution work, you'll need also the packages "timetk" and "janitor".

I don't know if Coingecko is the right source, because they only have "open" and "close" but not "high" and "low"...

@augur2
Copy link
Author

augur2 commented Jan 6, 2021

Found this: https://stackoverflow.com/questions/65514076/getting-no-data-when-scraping-a-table

you can scrape the cmc data for free without registration via their API.

Maybe this can be implemented in the crypto_history function...

@deanfantazzini
Copy link

I corrected the "download_coinmarketcap_daily" function in my package bitcoinFinannce + I added another function named "bitcoincharts_download_large" which is specifically suited for large files from bitcoincharts:
https://github.com/deanfantazzini/bitcoinFinance

@andreltr
Copy link

Hello,

I'm facing a related issue with crypto_history in the crypto library. From its example code,

R> library(crypto)
Loading required package: xml2

R> will_i_get_rich <- crypto_history(limit=10)
♥ If this helps you become rich please consider donating

ERC-20: 0x375923Bf82F0b728d23A5704261a6e16341fd860
XRP: rK59semLsuJZEWftxBFhWuNE6uhznjz2bK

❯ Scraping historical crypto data

Error in table$props$initialState$cryptocurrency :
$ operator is invalid for atomic vectors

Could anyone shed light regarding how to solve this problem, please?

TIA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants