python爬虫爬取天气数据
时间: 2023-08-29 13:06:45 浏览: 67
您好!可以使用Python中的requests和BeautifulSoup库来爬取天气数据。以下是基本的爬取流程:
1. 使用requests库发送get请求获取页面内容。
2. 使用BeautifulSoup库解析页面内容,获取所需信息。
3. 根据需要进行数据处理和存储。
下面是一个简单的例子,演示如何爬取北京的天气数据:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.weather.com.cn/weather/101010100.shtml'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
response = requests.get(url, headers=headers)
response.encoding = 'utf-8'
html = response.text
soup = BeautifulSoup(html, 'html.parser')
weather_list = soup.select('.t li')
for weather in weather_list:
date = weather.select('.date')[0].text.strip()
weather_info = weather.select('.wea')[0].text.strip()
temperature = weather.select('.tem span')[0].text.strip() + '℃'
print(date, weather_info, temperature)
```
以上代码使用requests库发送get请求获取北京天气网的页面内容,并使用BeautifulSoup库解析页面内容。通过CSS选择器获取日期、天气信息和温度,并进行打印输出。
需要注意的是,由于天气网是动态加载的,所以在爬取时可能需要使用Selenium等工具模拟浏览器行为,或者找到可用API接口进行数据获取。