Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import the necessary libraries & modules
- from bs4 import BeautifulSoup
- import requests
- #link to be scraped assigned to a variable
- wiki_page = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
- #A function that gets the URL of the page to be scraped
- #,gets the html content and uses BeautifulSoup to parse html content
- def make_soup(link):
- get_page = requests.get(link)
- html = get_page.content
- soup = BeautifulSoup(html, 'html.parser')
- return soup
- soup = make_soup(wiki_page)
- table = soup.find('table', attrs={'class': 'wikitable'}) # find the table
- tickers = []
- for row in table.find_all('tr')[1:]: # iterate over the rows, starting from
- # the second (first one is the header row)
- symbol_cell = row.find_all('td')[0] # the Symbol col is the first <td> in every row
- tickers.append(symbol_cell.text)
- print (tickers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement