-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmaster.py
More file actions
36 lines (27 loc) · 1.03 KB
/
master.py
File metadata and controls
36 lines (27 loc) · 1.03 KB
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 requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import squarify
url = "https://companiesmarketcap.com/dow-jones/largest-companies-by-market-cap/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "lxml")
rows = soup.findChildren("tr")
symbols = []
market_caps = []
sizes = []
for row in rows:
try:
symbol = row.find("div", {"class": "company-code"}).text
market_cap = row. findAll('td') [2].text
market_caps.append(market_cap)
symbols.append(symbol)
if market_cap.endswith("T"):
sizes.append(float(market_cap[1:-2]) * 10 ** 12)
elif market_cap.endswith("B"):
sizes.append(float(market_cap[1:-2]) * 10 ** 9)
except AttributeError:
pass
labels = [f"{symbols[i]}\n ({market_caps [i]})" for i in range(len(symbols))]
colors = [plt.cm.Set2(i / float(len(symbols))) for i in range(len(symbols)) ]
squarify.plot(sizes=sizes, label=labels, color=colors, bar_kwargs={"linewidth": 0.5, "edgecolor": "#111111"})
plt.show()