|
1 | | -from bs4 import BeautifulSoup |
2 | | -import requests |
3 | | - |
4 | | - |
5 | | -class Crypto: |
6 | | - """ |
7 | | - Create an instance of `Crypto` class |
8 | | -
|
9 | | - ```python |
10 | | - crypto = Crypto() |
11 | | - ``` |
12 | | -
|
13 | | - | Method | Details | |
14 | | - | ---------------------------- | -------------------------------------------------------- | |
15 | | - | `get_top_cryptocurrencies()` | Fetches and returns data about the top cryptocurrencies. | |
16 | | - """ |
17 | | - |
18 | | - def __init__(self): |
19 | | - """ |
20 | | - Initialize the CoinMarketCap class by fetching data from the CoinMarketCap website. |
21 | | - """ |
22 | | - headers = { |
23 | | - "User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win 64 ; x64) Apple WeKit /537.36(KHTML , like Gecko) Chrome/80.0.3987.162 Safari/537.36" |
24 | | - } |
25 | | - url = "https://coinmarketcap.com/" |
26 | | - html_text = requests.get(url, headers=headers).text |
27 | | - self.soup = BeautifulSoup(html_text, "lxml") |
28 | | - |
29 | | - def get_top_cryptocurrencies(self): |
30 | | - """ |
31 | | - A list of dictionaries containing details of the top cryptocurrencies.\n |
32 | | - ```python |
33 | | - crypto = Crypto() |
34 | | - ``` |
35 | | - Example output: |
36 | | - ```python |
37 | | - [ |
38 | | - { |
39 | | - "Name": "Bitcoin", |
40 | | - "Symbol": "BTC", |
41 | | - "Link": "https://coinmarketcap.com/...", |
42 | | - "Price": "$65,432.10", |
43 | | - "1h%": "-1.23% (Down)", |
44 | | - "24h%": "+0.45% (Up)", |
45 | | - "7d%": "-2.15% (Down)", |
46 | | - "MarketCap": "$1.23T", |
47 | | - "Volume(24h)": "$12.5B", |
48 | | - "Circulating Supply": "18.7M BTC" |
49 | | - }, |
50 | | - ... |
51 | | - ] |
52 | | - """ |
53 | | - try: |
54 | | - cryptocurrency = [] |
55 | | - container = self.soup.find("div", {"class": "sc-4c520df-2 kGWYlx"}) |
56 | | - i = 0 |
57 | | - tbody = container.find("tbody") |
58 | | - for items in tbody.find_all("tr"): |
59 | | - i += 1 |
60 | | - if i == 11: |
61 | | - break |
62 | | - j = 0 |
63 | | - for item in items.find_all("td"): |
64 | | - j += 1 |
65 | | - if j == 1 or j == 2: |
66 | | - continue |
67 | | - elif j == 3: |
68 | | - name = item.find("p", {"class": "sc-4984dd93-0 kKpPOn"}).text |
69 | | - symbol = item.find( |
70 | | - "p", {"class": "sc-4984dd93-0 iqdbQL coin-item-symbol"} |
71 | | - ).text |
72 | | - link = ( |
73 | | - "https://coinmarketcap.com/" |
74 | | - + item.find("a", href=True)["href"] |
75 | | - ) |
76 | | - elif j == 4: |
77 | | - price = item.text |
78 | | - elif j == 5: |
79 | | - if item.find("span", {"class": "icon-Caret-down"}) is not None: |
80 | | - market = "Down" |
81 | | - else: |
82 | | - market = "Up" |
83 | | - hour = item.text + f" ({market})" |
84 | | - elif j == 6: |
85 | | - if item.find("span", {"class": "icon-Caret-down"}) is not None: |
86 | | - market = "Down" |
87 | | - else: |
88 | | - market = "Up" |
89 | | - hour_24 = item.text + f" ({market})" |
90 | | - elif j == 7: |
91 | | - if item.find("span", {"class": "icon-Caret-down"}) is not None: |
92 | | - market = "Down" |
93 | | - else: |
94 | | - market = "Up" |
95 | | - day = item.text + f" ({market})" |
96 | | - elif j == 8: |
97 | | - marketcap = item.find( |
98 | | - "span", {"class": "sc-f8982b1f-1 bOsKfy"} |
99 | | - ).text |
100 | | - elif j == 9: |
101 | | - volume = item.find( |
102 | | - "p", {"class": "sc-4984dd93-0 jZrMxO font_weight_500"} |
103 | | - ).text |
104 | | - elif j == 10: |
105 | | - supply = item.find("p", {"class": "sc-4984dd93-0 WfVLk"}).text |
106 | | - data = { |
107 | | - "Name": name, |
108 | | - "Symbol": symbol, |
109 | | - "Link": link, |
110 | | - "Price": price, |
111 | | - "1h%": hour, |
112 | | - "24h%": hour_24, |
113 | | - "7d%": day, |
114 | | - "MarketCap": marketcap, |
115 | | - "Volume(24h)": volume, |
116 | | - "Circulating Supply": supply, |
117 | | - } |
118 | | - cryptocurrency.append(data) |
119 | | - return cryptocurrency |
120 | | - except: |
121 | | - return None |
| 1 | +from bs4 import BeautifulSoup |
| 2 | +import requests |
| 3 | + |
| 4 | + |
| 5 | +class Crypto: |
| 6 | + """ |
| 7 | + Create an instance of `Crypto` class |
| 8 | +
|
| 9 | + ```python |
| 10 | + crypto = Crypto() |
| 11 | + ``` |
| 12 | +
|
| 13 | + | Method | Details | |
| 14 | + | ---------------------------- | -------------------------------------------------------- | |
| 15 | + | `get_top_cryptocurrencies()` | Fetches and returns data about the top cryptocurrencies. | |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__(self): |
| 19 | + """ |
| 20 | + Initialize the CoinMarketCap class by fetching data from the CoinMarketCap website. |
| 21 | + """ |
| 22 | + headers = { |
| 23 | + "User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win 64 ; x64) Apple WeKit /537.36(KHTML , like Gecko) Chrome/80.0.3987.162 Safari/537.36" |
| 24 | + } |
| 25 | + url = "https://coinmarketcap.com/" |
| 26 | + html_text = requests.get(url, headers=headers).text |
| 27 | + self.soup = BeautifulSoup(html_text, "lxml") |
| 28 | + |
| 29 | + def get_top_cryptocurrencies(self): |
| 30 | + """ |
| 31 | + A list of dictionaries containing details of the top cryptocurrencies.\n |
| 32 | + ```python |
| 33 | + crypto = Crypto() |
| 34 | + ``` |
| 35 | + Example output: |
| 36 | + ```python |
| 37 | + [ |
| 38 | + { |
| 39 | + "Name": "Bitcoin", |
| 40 | + "Symbol": "BTC", |
| 41 | + "Link": "https://coinmarketcap.com/...", |
| 42 | + "Price": "$65,432.10", |
| 43 | + "1h%": "-1.23% (Down)", |
| 44 | + "24h%": "+0.45% (Up)", |
| 45 | + "7d%": "-2.15% (Down)", |
| 46 | + "MarketCap": "$1.23T", |
| 47 | + "Volume(24h)": "$12.5B", |
| 48 | + "Circulating Supply": "18.7M BTC" |
| 49 | + }, |
| 50 | + ... |
| 51 | + ] |
| 52 | + """ |
| 53 | + try: |
| 54 | + cryptocurrency = [] |
| 55 | + container = self.soup.find("div", {"class": "sc-4c520df-2 kGWYlx"}) |
| 56 | + i = 0 |
| 57 | + tbody = container.find("tbody") |
| 58 | + for items in tbody.find_all("tr"): |
| 59 | + i += 1 |
| 60 | + if i == 11: |
| 61 | + break |
| 62 | + j = 0 |
| 63 | + for item in items.find_all("td"): |
| 64 | + j += 1 |
| 65 | + if j == 1 or j == 2: |
| 66 | + continue |
| 67 | + elif j == 3: |
| 68 | + name = item.find("p", {"class": "sc-4984dd93-0 kKpPOn"}).text |
| 69 | + symbol = item.find( |
| 70 | + "p", {"class": "sc-4984dd93-0 iqdbQL coin-item-symbol"} |
| 71 | + ).text |
| 72 | + link = ( |
| 73 | + "https://coinmarketcap.com/" |
| 74 | + + item.find("a", href=True)["href"] |
| 75 | + ) |
| 76 | + elif j == 4: |
| 77 | + price = item.text |
| 78 | + elif j == 5: |
| 79 | + if item.find("span", {"class": "icon-Caret-down"}) is not None: |
| 80 | + market = "Down" |
| 81 | + else: |
| 82 | + market = "Up" |
| 83 | + hour = item.text + f" ({market})" |
| 84 | + elif j == 6: |
| 85 | + if item.find("span", {"class": "icon-Caret-down"}) is not None: |
| 86 | + market = "Down" |
| 87 | + else: |
| 88 | + market = "Up" |
| 89 | + hour_24 = item.text + f" ({market})" |
| 90 | + elif j == 7: |
| 91 | + if item.find("span", {"class": "icon-Caret-down"}) is not None: |
| 92 | + market = "Down" |
| 93 | + else: |
| 94 | + market = "Up" |
| 95 | + day = item.text + f" ({market})" |
| 96 | + elif j == 8: |
| 97 | + marketcap = item.find( |
| 98 | + "span", {"class": "sc-f8982b1f-1 bOsKfy"} |
| 99 | + ).text |
| 100 | + elif j == 9: |
| 101 | + volume = item.find( |
| 102 | + "p", {"class": "sc-4984dd93-0 jZrMxO font_weight_500"} |
| 103 | + ).text |
| 104 | + elif j == 10: |
| 105 | + supply = item.find("p", {"class": "sc-4984dd93-0 WfVLk"}).text |
| 106 | + data = { |
| 107 | + "Name": name, |
| 108 | + "Symbol": symbol, |
| 109 | + "Link": link, |
| 110 | + "Price": price, |
| 111 | + "1h%": hour, |
| 112 | + "24h%": hour_24, |
| 113 | + "7d%": day, |
| 114 | + "MarketCap": marketcap, |
| 115 | + "Volume(24h)": volume, |
| 116 | + "Circulating Supply": supply, |
| 117 | + } |
| 118 | + cryptocurrency.append(data) |
| 119 | + return cryptocurrency |
| 120 | + except: |
| 121 | + return None |
0 commit comments