pythonweb-scrapingbeautifulsouppython-requests

Scrape the latitude and longitude from the website


I want to convert a list of zip codes into a DataFrame of latitude and longitude using data from this website: Free Map Tools.

https://www.freemaptools.com/convert-us-zip-code-to-lat-lng.htm#google_vignette

Here’s my code, but it’s not returning the latitude and longitude data. How can I improve it ?

import requests
from bs4 import BeautifulSoup

def get_lat_lng(zip_code):
    # URL of the form processing page
    url = 'https://www.freemaptools.com/convert-us-zip-code-to-lat-lng.htm'
    
    # Create a session to handle cookies and headers
    session = requests.Session()
    
    # Send a GET request to get the initial form and any hidden data
    response = session.get(url)
    response.raise_for_status()
    
    # Parse the page with BeautifulSoup
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # Find form data (if needed)
    # Note: The actual form data extraction depends on how the website is structured
    # For simplicity, assume there's no hidden form data to worry about
    
    # Prepare the data to send in the POST request
    data = {
        'zip': zip_code
        # Include any other required form fields here if necessary
    }
    
    # Send a POST request with the zip code data
    response = session.post(url, data=data)
    response.raise_for_status()
    
    # Parse the resulting page
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # Extract latitude and longitude (you need to adjust these selectors based on the website's      structure)
    lat = soup.find('span', {'id': 'latitude'}).text.strip()
    lng = soup.find('span', {'id': 'longitude'}).text.strip()
    
    return lat, lng


# Example 
zip_code = ['97048','63640','63628']
latitude, longitude = get_lat_lng(zip_code)
print(f'Latitude: {latitude}, Longitude: {longitude}')
  1. query latitude and longitude data from https://www.freemaptools.com/convert-us-zip-code-to-lat-lng.htm#google_vignette

  2. Querying a list of zip code,i.e. ['97048','63640','63628'], and obtain the latitude and longitude for each zip.

  3. It results in error message.


Solution

  • Try:

    import requests
    
    api_url = (
        "https://api.promaptools.com/service/us/zip-lat-lng/get/?zip={}&key=17o8dysaCDrgvlc"
    )
    
    zips = ["97048", "63640", "63628"]
    
    headers = {
        "Origin": "https://www.freemaptools.com",
    }
    
    for z in zips:
        url = api_url.format(z)
        data = requests.get(url, headers=headers).json()
        print(z, data)
    

    Prints:

    97048 {'status': 1, 'output': [{'zip': '97048', 'latitude': '46.053228', 'longitude': '-122.971330'}]}
    63640 {'status': 1, 'output': [{'zip': '63640', 'latitude': '37.747435', 'longitude': '-90.363484'}]}
    63628 {'status': 1, 'output': [{'zip': '63628', 'latitude': '37.942778', 'longitude': '-90.484430'}]}