python百度街景数据爬取示例代码
时间: 2024-04-25 10:18:15 浏览: 12
当然,我可以给你提供一个Python百度街景数据爬取的示例代码。以下是一个简单的示例:
```python
import requests
import os
def download_street_view_images(location, output_dir):
url = f"http://api.map.baidu.com/panorama/v2?ak=your_api_key&width=512&height=256&location={location}&fov=360"
response = requests.get(url)
data = response.json()
if data["status"] == 0:
for i, street_view in enumerate(data["links"]):
image_url = street_view["link"]
image_name = f"{location}_{i}.jpg"
image_path = os.path.join(output_dir, image_name)
response = requests.get(image_url)
with open(image_path, "wb") as f:
f.write(response.content)
print(f"Downloaded image: {image_name}")
else:
print("Failed to retrieve street view data.")
# 示例用法
location = "39.915,116.404" # 经纬度坐标,这里以北京天安门为例
output_dir = "street_view_images" # 图片保存目录
download_street_view_images(location, output_dir)
```
请注意,上述示例代码中的`your_api_key`需要替换为你自己的百度地图API密钥。此外,你还需要安装`requests`库来发送HTTP请求。
这个示例代码通过调用百度地图API获取指定位置的街景数据,并将街景图片保存到指定目录中。你可以根据自己的需求进行修改和扩展。