-
Notifications
You must be signed in to change notification settings - Fork 1
/
trending cryptocurrencies webscraper.py
68 lines (55 loc) · 1.82 KB
/
trending cryptocurrencies webscraper.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
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import pandas as pd
def get_data():
# get the trending page parsed data into soup format using driver
url = 'https://www.coingecko.com/en/coins/trending'
option = webdriver.ChromeOptions()
option.add_argument('headless')
driver = webdriver.Chrome(options=option)
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'lxml')
driver.close()
#get the pct change (daily) of the top 10 gainers
aa = soup.find_all('td', {'class': 'td-change24h change24h stat-percent text-center'})
aa = aa[0:10]
bb = [str(x) for x in aa]
pct = []
for x in bb:
x = x.split('aed":', 1)[1]
x = x.split(',', 1)[0]
x = round(float(x),3)
pct.append(x)
# get the names of the top 10 coins
aa = soup.find_all('span', {'class': 'd-lg-none font-bold'})
n = [str(x) for x in aa]
n = n[0:10]
for x in range(0, len(n)):
n[x] = n[x].replace('</span>', '')
n[x] = n[x].replace('<span class="d-lg-none font-bold">', '')
#get price
aa = soup.find_all('td', {'class': 'td-price price'})
aa = aa[0:10]
aa = [str(x) for x in aa]
p = []
for x in aa:
x = x.split('$', 1)[1]
x = x.split('<', 1)[0]
p.append(x)
#get volume
aa = soup.find_all('td', {'class': 'td-liquidity_score lit'})
aa = aa[0:10]
aa = [str(x) for x in aa]
v = []
for x in aa:
x = x.split('$', 1)[1]
x = x.split('<', 1)[0]
v.append(x)
#create dataframe to print
dataframe = {'Coin Names': n, 'Daily % Change': pct, 'Price': p, 'Daily Volume': v}
df = pd.DataFrame(dataframe)
print(df)
if __name__ == "__main__":
get_data()