python爬虫爬取天气数据完整代码
时间: 2024-01-06 17:06:08 浏览: 246
以下是一个基于Python的爬虫爬取天气数据的完整代码示例:
```python
import requests
from bs4 import BeautifulSoup
# 发送HTTP请求获取网页内容
url = "https://www.example.com/weather" # 替换为你要爬取的网页地址
response = requests.get(url)
html_content = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, "html.parser")
# 提取需要的天气数据
weather_data = []
weather_elements = soup.select("div.weather-info") # 替换为你要提取的天气数据的CSS选择器
for element in weather_elements:
date = element.select_one("span.date").text
temperature = element.select_one("span.temperature").text
description = element.select_one("span.description").text
weather_data.append({"date": date, "temperature": temperature, "description": description})
# 打印天气数据
for data in weather_data:
print("日期:", data["date"])
print("温度:", data["temperature"])
print("描述:", data["description"])
print()
```
请注意,上述代码仅为示例,实际使用时需要根据目标网页的结构和需要提取的数据进行相应的修改。
阅读全文