In this lesson, I am going to show you how to web scraping Tesla’s charging station location info (address, phone number, station name) from Tesla’s website with BeautifulSoup library in Python.

This is an exercise suits for all level of Python developers; whether if you are someone who just started with BeautifulSoup or someone who has experienced web scraping but want to see how to easily scrape some information from Tesla’s website.





Source Code:

import requests
from bs4 import BeautifulSoup

country = 'United States'.replace(' ', '+')
url = f'https://www.tesla.com/findus/list/superchargers/{country}'

response = requests.get(url, headers={'user-agent': 'firefox'})
soup = BeautifulSoup(response.content, 'html.parser')

states = soup.find_all('div', {'class': 'state'})

for state in states:
    for vcard in state.find_all('address', {'class': 'vcard'}):
        try: 
            if not '(coming soon)' in vcard.text:
                print('Street Address: {0}'.format(vcard.find('span', {'class': 'street-address'}).text))
                print('Locaility: {0}'.format(vcard.find('span', {'class': 'locality'}).text))
                print('Phone #: {0}'.format(vcard.find('span', {'class': 'value'}).text))
                print('')
            else:
                print('Street Address: {0}(coming soon)'.format(vcard.find('span', {'class': 'locality'}).text))
                print('')
        except:
            pass