使用Python的urllib库和BeautifulSoup库爬取长春天气预报数据的代码
时间: 2023-06-04 21:03:21 浏览: 146
基于python机器学习的长春地区的天气预报,优秀Machine Learing程序设计!
5星 · 资源好评率100%
以下是使用Python的urllib库和BeautifulSoup库爬取长春天气预报数据的代码:
```python
import urllib.request
from bs4 import BeautifulSoup
url = "http://www.weather.com.cn/weather/101060101.shtml"
response = urllib.request.urlopen(url)
html = response.read()
soup = BeautifulSoup(html, 'html.parser')
weather_div = soup.find('div', {'id': '7d'})
ul = weather_div.find('ul')
li = ul.find_all('li')
for day in li:
date = day.find('h1').string
weather = day.find_all('p')
temperature_high = weather[0].find('span').string
temperature_low = weather[1].find('span').string
print(date, temperature_high, temperature_low)
```
在运行代码之前,需要先安装BeautifulSoup库:
```python
pip install beautifulsoup4
```
阅读全文