Advertisement
wtgeographer

web2csv

Oct 19th, 2016
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. import os
  2.  
  3. os.chdir(r'C:\Users\<user>\Desktop')
  4.  
  5. from bs4 import BeautifulSoup
  6. import requests
  7. import pandas as pd
  8.  
  9. url = "https://www.akc.org/reg/dogreg_stats.cfm"
  10. r = requests.get(url)
  11. data = r.text
  12. soup = BeautifulSoup(data)
  13.  
  14. table = soup.find_all('table')[0]
  15. rows = table.find_all('tr')[2:]
  16.  
  17. data = {
  18.     'breeds' : [],
  19.     'rank2015' : [],
  20.     'rank2014' : [],
  21.     'rank2013' : []
  22. }
  23.  
  24. for row in rows:
  25.     cols = row.find_all('td')
  26.     data['breeds'].append( cols[0].get_text().encode('utf-8').strip() )
  27.     data['rank2015'].append( cols[1].get_text().encode('utf-8').strip() )
  28.     data['rank2014'].append( cols[2].get_text().encode('utf-8').strip() )
  29.     data['rank2013'].append( cols[3].get_text().encode('utf-8').strip() )
  30.  
  31. dogData = pd.DataFrame( data )
  32. dogData.to_csv("AKC_Dog_Registrations.csv")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement