用python代码实现爬取自己嘉兴10日内的天气预报
时间: 2024-01-26 16:24:24 浏览: 27
好的,以下是示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.weather.com.cn/weather/101210301.shtml' # 嘉兴市天气预报页面的链接
response = requests.get(url)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'html.parser')
days = soup.select('.day') # 获取未来7天的日期
weathers = soup.select('.wea') # 获取未来7天的天气状况
temperatures = soup.select('.tem') # 获取未来7天的温度区间
for i in range(7):
print(days[i].text, weathers[i].text, temperatures[i].text)
```
运行此代码即可输出嘉兴市未来7天的天气预报。如果需要获取10天内的预报,则需要修改链接中的数字,例如:
- 101210301:嘉兴市
- 101210401:桐乡市
- 101210412:海宁市
- 101210403:嘉善县
- 101210404:海盐县
根据需要选择对应的数字即可。
相关问题
用python代码实现爬取自己嘉兴10日内的天气预报并生成一个网站
好的,以下是一个简单的Python爬虫示例,可以爬取嘉兴市某个气象网站的天气预报数据,并将数据存储到本地的SQLite数据库中。同时,还可以使用Flask框架开发一个简单的天气预报网站,展示最近10天的天气信息。
1. 确定数据来源:我们可以使用中国天气网(http://www.weather.com.cn)提供的天气预报数据,具体的URL是:http://www.weather.com.cn/weather/101210301.shtml
2. 编写爬虫程序:我们可以使用Python的requests和BeautifulSoup库来实现爬虫程序。具体的代码如下:
```python
import requests
from bs4 import BeautifulSoup
import sqlite3
# 爬取天气预报数据
def get_weather_data():
url = 'http://www.weather.com.cn/weather/101210301.shtml'
r = requests.get(url)
r.encoding = 'utf-8'
soup = BeautifulSoup(r.text, 'html.parser')
# 获取最近10天的天气预报数据
weather_list = []
for i in range(1, 11):
date_str = soup.select_one(f'#day_{i} > h1').text
date = date_str.split('(')[0]
weather = soup.select_one(f'#day_{i} > p.wea').text
temperature = soup.select_one(f'#day_{i} > p.tem').text
weather_list.append((date, weather, temperature))
return weather_list
# 存储天气预报数据到SQLite数据库
def save_to_database(weather_list):
conn = sqlite3.connect('weather.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS weather
(date text, weather text, temperature text)''')
c.execute('DELETE FROM weather')
c.executemany('INSERT INTO weather VALUES (?, ?, ?)', weather_list)
conn.commit()
conn.close()
if __name__ == '__main__':
weather_list = get_weather_data()
save_to_database(weather_list)
```
3. 开发网站:我们可以使用Flask框架来实现一个简单的天气预报网站。具体的代码如下:
```python
from flask import Flask, render_template
import sqlite3
app = Flask(__name__)
# 获取天气预报数据
def get_weather_data():
conn = sqlite3.connect('weather.db')
c = conn.cursor()
c.execute('SELECT * FROM weather')
weather_list = c.fetchall()
conn.close()
return weather_list
# 渲染天气预报网站页面
@app.route('/')
def index():
weather_list = get_weather_data()
return render_template('index.html', weather_list=weather_list)
if __name__ == '__main__':
app.run()
```
4. 前端设计:我们可以使用HTML和CSS来实现网站页面的设计,同时使用Jinja2模板来渲染数据。具体的代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>嘉兴天气预报</title>
<style>
table {
border-collapse: collapse;
width: 80%;
margin: auto;
}
th, td {
border: 1px solid black;
text-align: center;
padding: 10px;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>嘉兴天气预报</h1>
<table>
<tr>
<th>日期</th>
<th>天气</th>
<th>温度</th>
</tr>
{% for weather in weather_list %}
<tr>
<td>{{ weather[0] }}</td>
<td>{{ weather[1] }}</td>
<td>{{ weather[2] }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
```
5. 部署网站:将以上代码保存到一个目录中,启动Python虚拟环境,安装所需的依赖库,并运行`python app.py`即可启动网站。需要注意的是,生产环境中应该使用Gunicorn或者uWSGI等Web服务器来部署网站,并使用Nginx或者Apache等反向代理服务器进行负载均衡和安全保护。
以上是一个简单的Python爬虫和网站开发示例,具体的实现过程可能会有所不同。希望对您有所帮助。
python爬取自己家乡10日内的天气预报
好的,这个问题可以使用 Python 中的 requests 和 BeautifulSoup 库来实现。首先需要找到一个提供天气预报信息的网站,例如中国天气网。
以下是一个简单的代码示例,可以爬取中国天气网上自己家乡10日内的天气预报:
```python
import requests
from bs4 import BeautifulSoup
# 设置城市名称和城市代码
city_name = "xxx" # 替换成你自己的城市名称
city_code = "xxx" # 替换成你自己的城市代码
# 构造请求URL
url = f"http://www.weather.com.cn/weather/{city_code}.shtml"
# 发送请求
response = requests.get(url)
# 解析HTML内容
soup = BeautifulSoup(response.content, "html.parser")
weather_items = soup.find_all("div", class_="t")
# 输出未来10天的天气预报
for item in weather_items:
date = item.find("h1").text
weather = item.find(class_="wea").text
temperature = item.find(class_="tem").text.strip()
print(f"{date},{weather},{temperature}")
```
需要注意的是,每个城市的代码不同,需要根据实际情况进行替换。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)