Still trying to learn Python. As a learning exercise, I tried to write a script to pull the last 5 years of historical bitcoin data from coinmarketcap.com. I have it so no errors are generated whatsoever, but no CSV file generates at the end. I read elsewhere it may save to another directory, but I can see the working directory is where I'm looking, and I used df.to_csv in another script in this directory and it saved correctly.
I am using Spyder through Anaconda Notebooks. Here is my script:
from bs4 import BeautifulSoup
import requests
import pandas as pd
import json
coins = {}
for i in coins:
page = requests.get('https://coinmarketcap.com/currencies/bitcoin/historical-data/?start=20150920&end=20200918')
soup = BeautifulSoup(page.content, 'html.parser')
data = soup.find('script', id="__NEXT_DATA__",type="application/json")
historical_data = json.loads(data.contents[0])
quotes = historical_data['props']['initialState']['cryptocurrency']['ohlcvHistorical']['i']['quotes']
market_cap = []
volume = []
timestamp = []
name = []
symbol = []
slug = []
df = pd.DataFrame(columns = ['marketcap','volume','timestamp','name','symbol','slug'])
for j in quotes:
market_cap.append(j['quote']['USD']['market_cap'])
volume.append(j['quote']['USD']['volume'])
timestamp.append(j['quote']['USD']['timestamp'])
name.append(info['name'])
symbol.append(info['symbol'])
slug.append(coins[i])
df['marketcap'] = market_cap
df['volume'] = volume
df['timestamp'] = timestamp
df['name'] = name
df['symbol'] = symbol
df['slug'] = slug
df.to_csv('C:\\Python34\\Projects\\Trends\\btcvalue.csv', index=False)
Your first for loop will never run, because coins
is always empty. And I would suggest to move df.to_csv
out of the for loop.
[...]
coins = {}
for i in coins:
[...]
df['slug'] = slug
df.to_csv('C:\\Python34\\Projects\\Trends\\btcvalue.csv', index=False)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.