从网站上抓取纬度和经度

问题描述 投票:0回答: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','63628'],并获取每个邮政编码的纬度和经度。

  3. 这会导致错误消息。

python-3.x web-scraping beautifulsoup python-requests
1个回答
0
投票

尝试:

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'}]}
© www.soinside.com 2019 - 2024. All rights reserved.