代码之家  ›  专栏  ›  技术社区  ›  Canberra

从网站上删除纬度和经度

  •  2
  • Canberra  · 技术社区  · 1 年前

    我想使用来自以下网站的数据将邮政编码列表转换为纬度和经度的DataFrame:免费地图工具。

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

    这是我的代码,但它没有返回纬度和经度数据。我该如何改进它?

    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. 从以下位置查询纬度和经度数据 https://www.freemaptools.com/convert-us-zip-code-to-lat-lng.htm#google_vignette

    2. 查询邮政编码列表,即['97048'、'63640'、'63528'],并获取每个邮政编码的纬度和经度。

    3. 这会导致错误消息。

    1 回复  |  直到 1 年前
        1
  •  1
  •   Andrej Kesely    1 年前

    尝试:

    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)
    

    打印:

    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'}]}