-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscrape.py
More file actions
36 lines (28 loc) · 858 Bytes
/
scrape.py
File metadata and controls
36 lines (28 loc) · 858 Bytes
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
import urllib.request
import json
from datetime import date
from bs4 import BeautifulSoup
opener = urllib.request.FancyURLopener({})
url = "https://coinmarketcap.com/"
f = opener.open(url)
content = f.read()
soup = BeautifulSoup(content, 'html.parser')
table = soup.select('div.cmc-table tbody', class_="cmc-table")[0]
trs = table.find_all('tr')
coins = {}
for tr in trs: # Loop over all the TRs in the tbody
tdName = tr.select('tr td:nth-child(2)')
if not tdName: continue # Probably an ad
name = tdName[0].get_text()
tdPrice = tr.select('tr td:nth-child(4)')
price = float(
tdPrice[0].get_text()
.replace('$', '')
.replace(',', '')
)
coins[name] = price
with open('coins.json') as read:
data = json.load(read)
data[str(date.today())] = coins
with open('coins.json', 'w') as outfile:
json.dump(data, outfile)