Scraping Coordinates with Beautiful Soup


/ Published in: Python
Save to your folder(s)

scraping geographical coordinates from an online directory (for a mapping project), using Beautiful Soup. Data exported into csv.


Copy this code and paste it in your HTML
  1. #! python3
  2. #import modules
  3. from bs4 import BeautifulSoup
  4. import pandas as pd
  5.  
  6. #open downloaded html, adjust encoding if does't look right
  7. with open("webpage.html", encoding='utf-8') as fp:
  8. soup = BeautifulSoup(fp, features="html.parser")
  9.  
  10. #create empty lists for desired data
  11. names = []
  12. addresses = []
  13. latitude = []
  14. longitude = []
  15.  
  16. #loop through document
  17. #fist argument in find_all is the name of element, then attributes
  18. #for each card, get desired data and push into relateed list
  19. for div in soup.find_all("div", class_="card-component place-card"):
  20. names.append(div.get('data-name'))
  21. addresses.append(div.get('data-address'))
  22. latitude.append(div.get('data-latitude'))
  23. longitude.append(div.get('data-longitude'))
  24.  
  25. #create panda dataframe based on python lists, then translate into csv
  26. df = pd.DataFrame({'Pharmacies':names,'Location':addresses,'Latitude':latitude, 'Longitude': longitude})
  27. df.to_csv('ahumada.csv', index=False, encoding='latin-1')

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.