桂林天气python
时间: 2023-11-11 18:59:57 浏览: 189
桂林的天气可以通过爬取天气网站或者调用天气API来获取。以下是通过调用天气API获取桂林天气的Python代码示例:
```python
import requests
url = "https://api.seniverse.com/v3/weather/now.json"
params = {
"key": "your_api_key",
"location": "guilin",
"language": "zh-Hans",
"unit": "c"
}
response = requests.get(url, params=params)
data = response.json()
weather = data["results"][0]["now"]["text"]
temperature = data["results"][0]["now"]["temperature"]
print("桂林现在的天气是{},温度是{}℃。".format(weather, temperature))
```
请注意,上述代码中的 `your_api_key` 需要替换为你自己的API Key。
相关问题
把天气爬取下来后用fastapi封装起来,用ECharts查询15天气信息,其中101191101代表常州市。 部分测试城市代号如下,将其放到下拉框中,选择显示该城市的15天天气: 101010100,北京 101020100,上海 101190401,苏州 101190201,无锡 101190101,南京 101190601,扬州 101190301,镇江 101240501,宜春 101070101,沈阳 101070601,丹东 101080701,鄂尔多斯 101081001,呼伦贝尔 101160701,张掖 101280101,广州 101280601,深圳 101300501,桂林 101300101,南宁 101260801,六盘水 101260506,黔东南 101260201,遵义 101310101,海口 101310201,三亚 101310222,五指山 101180801,开封 101180901,洛阳 101251101,张家界 101221001,黄山 101320101,香港 101330101,澳门
好的,我可以帮你实现这个功能。首先需要爬取天气信息,我可以使用Python的requests和BeautifulSoup库来进行爬取。以下是代码示例:
```python
import requests
from bs4 import BeautifulSoup
def get_weather(city_code):
url = f'http://www.weather.com.cn/weather/{city_code}.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 Edge/16.16299',
'Referer': f'http://www.weather.com.cn/weather/{city_code}.shtml'
}
res = requests.get(url, headers=headers)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')
weather_list = []
for i in range(15):
date = soup.select('.tqtongji1 ul li h1')[i].text
weather = soup.select('.tqtongji1 ul li p')[i].text.replace('\n', '')
temperature = soup.select('.tqtongji1 ul li span')[i].text
weather_list.append({'date': date, 'weather': weather, 'temperature': temperature})
return weather_list
```
接下来,我们可以使用FastAPI来封装这个接口。以下是代码示例:
```python
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
app = FastAPI()
app.mount('/static', StaticFiles(directory='static'), name='static')
templates = Jinja2Templates(directory='templates')
@app.get('/', response_class=HTMLResponse)
async def index():
return templates.TemplateResponse('index.html', {'request': request})
@app.get('/weather/{city_code}', response_class=HTMLResponse)
async def weather(city_code, request: Request):
weather_list = get_weather(city_code)
return templates.TemplateResponse('weather.html', {'request': request, 'city_code': city_code, 'weather_list': weather_list})
```
最后,我们可以使用ECharts来展示天气信息。以下是HTML模板的代码示例:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>15天天气预报</title>
<script src="https://cdn.staticfile.org/echarts/4.8.0/echarts.min.js"></script>
</head>
<body>
<div id="main" style="width: 100%;height:600px;"></div>
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('main'));
var option = {
title: {
text: '15天天气预报'
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['最高气温', '最低气温']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'category',
boundaryGap: false,
data: [
{% for item in weather_list %}
'{{ item.date }}',
{% endfor %}
]
},
yAxis: {
type: 'value'
},
series: [
{
name: '最高气温',
type: 'line',
data: [
{% for item in weather_list %}
'{{ item.temperature.split("/")[1].replace("℃","") }}',
{% endfor %}
],
markPoint: {
data: [
{type: 'max', name: '最大值'},
{type: 'min', name: '最小值'}
]
},
markLine: {
data: [
{type: 'average', name: '平均值'}
]
}
},
{
name: '最低气温',
type: 'line',
data: [
{% for item in weather_list %}
'{{ item.temperature.split("/")[0].replace("℃","") }}',
{% endfor %}
],
markPoint: {
data: [
{type: 'max', name: '最大值'},
{type: 'min', name: '最小值'}
]
},
markLine: {
data: [
{type: 'average', name: '平均值'}
]
}
}
]
};
myChart.setOption(option);
</script>
</body>
</html>
```
这样,当用户选择城市后,就可以展示该城市未来15天的天气信息了。
阅读全文