python爬虫九江天气预报
时间: 2023-11-21 08:07:00 浏览: 79
以下是使用Python爬虫获取九江天气预报的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 发送请求
url = 'http://www.weather.com.cn/weather/101240201.shtml'
response = requests.get(url)
# 解析HTML
soup = BeautifulSoup(response.content, 'html.parser')
weather_div = soup.find('div', {'id': '7d'})
# 获取天气信息
lis = weather_div.find('ul').find_all('li')
for li in lis:
date = li.find('h1').string
weather = li.find_all('p')[0].string
temperature = li.find_all('p')[1].find('span').string + '/' + li.find_all('p')[1].find('i').string
wind = li.find_all('p')[2].find('span')['title']
print(date + ' ' + weather + ' ' + temperature + ' ' + wind)
```
这段代码使用requests库向天气预报网站发送请求,然后使用BeautifulSoup库解析HTML页面,最后获取天气信息并输出。
阅读全文