Advertisement
Guest User

First Scrape

a guest
Jul 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. #import the necessary libraries & modules
  2. from bs4 import BeautifulSoup
  3. import requests
  4.  
  5.  
  6. #link to be scraped assigned to a variable
  7. wiki_page = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
  8.  
  9. #A function that gets the URL of the page to be scraped
  10. #,gets the html content and uses BeautifulSoup to parse html content
  11.  
  12. def make_soup(link):
  13.     get_page = requests.get(link)
  14.     html = get_page.content
  15.     soup = BeautifulSoup(html, 'html.parser')
  16.     return  soup
  17.  
  18. soup = make_soup(wiki_page)
  19.  
  20. table = soup.find('table', attrs={'class': 'wikitable'})  # find the table
  21. tickers = []
  22.  
  23. for row in table.find_all('tr')[1:]:  # iterate over the rows, starting from
  24.                                       # the second (first one is the header row)
  25.     symbol_cell = row.find_all('td')[0]  #  the Symbol col is the first <td> in every row
  26.     tickers.append(symbol_cell.text)
  27.  
  28. print (tickers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement