Created
November 27, 2023 15:49
-
-
Save bot-unit/09a2731bc92924548e7dfbacb9a10515 to your computer and use it in GitHub Desktop.
s-and-p-500-companies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
def get_table_from_slickcharts(url: str) -> List[Dict[str, Union[str, float]]]: | |
# parse table for symbols from slickcharts | |
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} | |
response = requests.get(url, headers=headers) | |
data = [] | |
if response.status_code == 200: | |
soup = BeautifulSoup(response.content, 'html.parser') | |
table = soup.find('table', attrs={'class': 'table'}) | |
tbody = table.find('tbody') | |
for row in tbody.findAll('tr'): | |
cols = row.findAll('td') | |
name_col = cols[1] | |
name = name_col.find('a').text | |
ticker_col = cols[2] | |
ticker = ticker_col.find('a').text | |
weight_col = cols[3] | |
weight = float(weight_col.text[:-1]) | |
data.append({'name': name, 'ticker': ticker, 'weight': weight}) | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment