python 爬取贵阳天气
时间: 2023-06-10 21:08:55 浏览: 185
可以使用 Python 的 requests 库和 BeautifulSoup 库来爬取贵阳天气。以下是一个示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.weather.com.cn/weather/101260101.shtml' # 贵阳天气预报页面的 URL
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# 获取天气预报信息
weather_list = soup.find('ul', class_='t clearfix').find_all('li')
# 打印天气预报信息
for weather in weather_list:
date = weather.find('h1').string
weather_desc = weather.find_all('p')
desc = weather_desc[0].string
temperature = weather_desc[1].find('span').string + '/' + weather_desc[1].find('i').string
wind = weather_desc[2].find('span')['title']
print(date + '\t' + desc + '\t' + temperature + '\t' + wind)
```
运行上述代码,即可输出贵阳未来几天的天气预报信息。
阅读全文