Created
March 11, 2022 13:43
-
-
Save rohithteja/3c480542e9c3545eaffbd717f3ec29a1 to your computer and use it in GitHub Desktop.
Web scraper for Yahoo Finance
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
| #------------------- | |
| # Web scraping Yahoo Finance | |
| #------------------- | |
| from bs4 import BeautifulSoup | |
| import requests | |
| import pandas as pd | |
| dic = {} | |
| url = 'https://finance.yahoo.com/cryptocurrencies?offset=0&count=100' | |
| soup = BeautifulSoup(requests.get(url).text) | |
| # store values in separate lists and then in a dictionary | |
| for listing in soup.find_all('div', attrs={'id':'fin-scr-res-table'}): | |
| symbol_list = [] | |
| name_list = [] | |
| price_list = [] | |
| change_list = [] | |
| mcap_list = [] | |
| for symbol in listing.find_all('td', attrs={'aria-label':'Symbol'}): | |
| symbol_list.append(symbol.text) | |
| dic['Symbol'] = symbol_list | |
| for name in listing.find_all('td', attrs={'aria-label':'Name'}): | |
| name_list.append(name.text) | |
| dic['Name'] = name_list | |
| for price in listing.find_all('td', attrs={'aria-label':'Price (Intraday)'}): | |
| price_list.append(price.text) | |
| dic['Price'] = price_list | |
| for change in listing.find_all('td', attrs={'aria-label':'% Change'}): | |
| change_list.append(change.text) | |
| dic['% Change'] = change_list | |
| for mcap in listing.find_all('td', attrs={'aria-label':'Market Cap'}): | |
| mcap_list.append(mcap.text) | |
| dic['Market Cap'] = mcap_list | |
| # create a dataframe from dictionary | |
| df_scrape = pd.DataFrame(dic) | |
| df_scrape.Symbol = df_scrape.Symbol.str.replace('-USD','') | |
| df_scrape.Name = df_scrape.Name.str.replace(' USD','') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment