0

This is a Script to gather historical cryptocurrency data from www.coinmarketcap.com from a specific date.I am getting on line 76 index out of range.I am getting this error list index out of range.I dont know what is causing this error.Plz help guys.Thanks.

Error----line 76, in startdate = sys.argv[1] IndexError: list index out of range

import json
    import requests
    from bs4 import BeautifulSoup
    import csv
    import sys

    def CoinNames():
        """Gets ID's of all coins on cmc"""

        names = []
        response = requests.get("https://api.coinmarketcap.com/v1/ticker/?limit=0")
        respJSON = json.loads(response.text)
        for i in respJSON:
            names.append(i['id'])
        return names

    def gather(startdate, enddate, names):
        historicaldata = []
        counter = 1

        if len(names) == 0:
            names = CoinNames()

        for coin in names:
            r  = requests.get("https://coinmarketcap.com/currencies/{0}/historical-data/?start={1}&end={2}".format(coin, startdate, enddate))
            data = r.text
            soup = BeautifulSoup(data, "html.parser")
            table = soup.find('table', attrs={ "class" : "table"})

            #Add table header to list
            if len(historicaldata) == 0:
                headers = [header.text for header in table.find_all('th')]
                headers.insert(0, "Coin")

            for row in table.find_all('tr'):
                currentrow = [val.text for val in row.find_all('td')]
                if(len(currentrow) != 0):
                    currentrow.insert(0, coin)
                historicaldata.append(currentrow)

            print("Coin Counter -> " + str(counter), end='\r')
            counter += 1
        return headers, historicaldata

    def _gather(startdate, enddate):
        """ Scrape data off cmc"""

        if(len(sys.argv) == 3):
            names = CoinNames()
        else:
            names = [sys.argv[3]]

        headers, historicaldata = gather(startdate, enddate, names)

        Save(headers, historicaldata)

    def Save(headers, rows):

        if(len(sys.argv) == 3):
            FILE_NAME = "HistoricalCoinData.csv"
        else:
            FILE_NAME = sys.argv[3] + ".csv"

        with open(FILE_NAME, 'w') as f:
            writer = csv.writer(f)
            writer.writerow(headers)
            writer.writerows(row for row in rows if row)
        print("Finished!")

    if __name__ == "__main__":

        startdate = sys.argv[1]
        enddate = sys.argv[1]

        _gather(startdate, enddate)
Amreign
  • 17
  • 4

1 Answers1

0

when you run this, with sys.argv, you have to pass arguments from the command line.

$ python script.py 20180101 

although I'm not sure of the format of the start date, and I'm not sure why there is no sys.argv[2] if there is a sys.argv[3]

see here for more info.

philshem
  • 24,761
  • 8
  • 61
  • 127
  • Thanks for ur reply.Sir this is a github project.This was mention in github. To run the script and gather data for all listed cryptocurrencys on coinmarketcap you need to pass the start date and end date in YYYYMMDD format $ python3 crypto_history.py 20170101 20180201 Please can u tell me in bit detail what does it mean where i should add the dates.I am a noob sir please help me.THANKU. – Amreign Mar 29 '19 at 19:16
  • Can you please tell me the way (command) you running the script? Actually, you have to pass at least 1 or at most 3 command line args as per your code. Just running script using `python crypto_script.py` will be a reason for that `IndexError` problem. – hygull Mar 30 '19 at 13:16
  • Can you post link to github source? – philshem Mar 30 '19 at 14:06
  • https://github.com/dylankilkenny/CoinMarketCap-Historical-Prices#running @philshem – Amreign Mar 30 '19 at 18:57