/
crawl-data.py
148 lines (102 loc) · 3.49 KB
/
crawl-data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# coding: utf-8
# In[10]:
import requests, bs4
url = "https://coinmarketcap.com/currencies/"
res = requests.get(url)
try:
res.raise_for_status()
except Exception as exc:
print('There was a problem: %s' % (exc))
soup = bs4.BeautifulSoup(res.text, "html5lib")
data_rows = soup.findAll('tr')
currencies = []
for i in range(1, 101):
currencies.append(data_rows[i].get('id')[3:])
#currencies
# In[15]:
import requests, bs4, os
for currency in currencies:
url = "https://coinmarketcap.com/currencies/" + currency + "/historical-data/?start=20000428&end=20171012"
res = requests.get(url)
soup = bs4.BeautifulSoup(res.text, "html5lib")
data_rows = soup.findAll('tr')
all_data = []
open_prices = []
high_prices = []
low_prices = []
close_prices = []
volumes = []
market_caps = []
for i in range(len(data_rows)):
row = []
datapoint = []
# go through each data element in each table row
for td in data_rows[i].findAll('td'):
# get text and append to row
row.append(td.getText())
# add row to matricies
if len(row) > 6:
open_prices.append(float(str(row[1])))
high_prices.append(float(str(row[2])))
low_prices.append(float(str(row[3])))
close_prices.append(float(str(row[4])))
volume = list(str(row[5]))
if volume[0] is "-":
volume = str(all_data[i-1][4])
volume = list(volume)
volume = [char for char in volume if (char is not "," and char is not u",")]
volume = "".join(volume)
volumes.append(float(volume))
cap = list(str(row[6]))
if cap[0] == '-':
cap = str(all_data[i-1][5])
cap = list(cap)
cap = [char for char in cap if (char is not "," and char is not u",")]
cap = "".join(cap)
market_caps.append(float(cap))
datapoint.append(float(str(row[1])))
datapoint.append(float(str(row[2])))
datapoint.append(float(str(row[3])))
datapoint.append(float(str(row[4])))
datapoint.append(volume)
datapoint.append(cap)
all_data.append(datapoint)
writefile = os.path.expanduser(currency+"_all_data"+".csv")
writer = open(writefile, "w")
writer.write("open, high, low, close, volume, cap")
writer.write('\n')
for line in all_data:
temp = ""
for element in line:
temp += str(element)
temp += ','
temp = temp[:-1]
writer.write(temp)
writer.write('\n')
writer.close
writefile = os.path.expanduser(currency+"_open"+".csv")
writer = open(writefile, "w")
for line in open_prices:
writer.write(str(line))
writer.write('\n')
writer.close
writefile = os.path.expanduser(currency+"_close"+".csv")
writer = open(writefile, "w")
for line in close_prices:
writer.write(str(line))
writer.write('\n')
writer.close
writefile = os.path.expanduser(currency+"_high"+".csv")
writer = open(writefile, "w")
for line in high_prices:
writer.write(str(line))
writer.write('\n')
writer.close
writefile = os.path.expanduser(currency+"_low"+".csv")
writer = open(writefile, "w")
for line in low_prices:
writer.write(str(line))
writer.write('\n')
writer.close
print("Scraped", currency)
# In[ ]: