skip to Main Content

I would like to get all physical addresses in MS-excel from this url [https://www.tamoil.ch/en/store-locator]. The spreadsheet only has a header, but no output from the code.

import requests
from bs4 import BeautifulSoup
import pandas as pd

# Send a GET request to the website
url = "https://www.tamoil.ch/en/store-locator"

response = requests.get(url)

# Parse the HTML content
soup = BeautifulSoup(response.content, 'html.parser')

# Find all elements containing store information
store_elements = soup.find_all('div', class_='store-element')

# Extract addresses
addresses = []
for store in store_elements:
    address = store.find('p', class_='address').text.strip()
    addresses.append(address)

# Create a DataFrame
df = pd.DataFrame({'Address': addresses})

# Save to Excel file
excel_file = 'tamoil_addresses.xlsx'
df.to_excel(excel_file, index=False)

print(f"Addresses saved to {excel_file}")

2

Answers


  1. Can you check this out if this is what you are looking for?

    import requests
    import pandas as pd
    from requests.packages.urllib3.exceptions import InsecureRequestWarning
    
    
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
    
    url = "https://www.tamoil.ch/page-data/en/store-locator/page-data.json" #address and location in JSON format
    
    resp = requests.get(url, verify=False)
    data = resp.json()['result']['data']['allStation']['nodes']
    
    slug_r = []
    name_r = []
    lat_r = []
    lng_r = []
    street_r = []
    zip_code_r = []
    city_r = []
    phone_r = []
    
    for i in data:
        slug = i['slug']
        name = i['name']
        lat = i['lat']
        lng = i['lng']
        street = i['street']
        zip_code = i['zip']
        city = i['city']['name']
        phone_1 = i['phone_1']
        
        #append 
        slug_r.append(slug)
        name_r.append(name)
        lat_r.append(lat)
        lng_r.append(lng)
        street_r.append(street)
        zip_code_r.append(zip_code)
        city_r.append(city)
        phone_r.append(phone_1)
        print(f"-----------------------nSLUG:   {slug}nNAME:   {name}nLATITUDE:   {lat}nLONITUDE:   {lng}nSTREET ADDRESS:   {street}nPHONE:   {phone_1}nZIP CODE:   {zip_code}nCITY:   {city}")
    
    data_dict = {
        'SLUG': slug_r,
        'NAME': name_r,
        'LATITUDE': lat_r,
        'LONGITUDE': lng_r,
        'STREET': street_r,
        'ZIP CODE': zip_code_r,
        'PHONE': phone_r,
        'CITY': city_r
    }
    df = pd.DataFrame(data_dict)
    df.to_excel('Address.xlsx', index=False)
    

    I used their page-data (JSON) endpoint to grab all these addresses.

    Let me know if I missed something based on your question

    Login or Signup to reply.
  2. All the information you need can be found here which means that you only have to parse a JSON document to get the required results.

    import requests
    import pandas as pd
    
    filename = "tamoil.xlsx"
    url = "https://www.tamoil.ch/page-data/en/store-locator/page-data.json"
    # add other elements of interest to this list
    keylist = ["name", "street", "zip"]
    
    def make_dict(node):
        return {key.title(): node.get(key, "n/a") for key in keylist}
    
    with requests.get(url) as response:
        response.raise_for_status()
        data = response.json()
        addresses = [make_dict(n) for n in data["result"]["data"]["allStation"]["nodes"]]
        df = pd.DataFrame(addresses)
        df.to_excel(filename, index=False)
        for address in addresses[:10]:  # sample
            print(address)
    

    Output:

    {'Name': 'TAMOIL Basel Aéroport', 'Street': 'Parkplatz 11 Aéroport', 'Zip': '4030'}
    {'Name': 'TAMOIL Bellwald', 'Street': 'Fuergangen', 'Zip': '3997'}
    {'Name': 'TAMOIL Commugny', 'Street': 'Rte de Genève 19', 'Zip': '1291'}
    {'Name': 'TAMOIL Flawil', 'Street': 'St.Galler-Strasse 144', 'Zip': '9230'}
    {'Name': 'TAMOIL Glarus', 'Street': 'Buchholzstrasse 44', 'Zip': '8750'}
    {'Name': 'TAMOIL Grandson', 'Street': 'Rue de Neuchâtel 43 / CP91', 'Zip': '1422'}
    {'Name': 'TAMOIL Hermance', 'Street': "564, rte D'Hermance", 'Zip': '1248'}
    {'Name': "TAMOIL L'Orient", 'Street': '20, rue du Pont-Neuf', 'Zip': '1341'}
    {'Name': 'TAMOIL Le Châble (Le Cotterg)', 'Street': 'Le Cotterg', 'Zip': '1934'}
    {'Name': 'TAMOIL Le Pont', 'Street': 'Sur les Quais 39', 'Zip': '1342'}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search