1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
"""
@Time : 2022/12/12 18:52
@Author : daokunn
@File :九江天气.py
@IDE :PyCharm
@Motto: Don’t cry over spilt milk.
功能: 查询九江当天的天气
"""
import requests
from bs4 import BeautifulSoup

def jj_weather():
# print('九江')
res = requests.get('http://www.weather.com.cn/weather/101010100.shtml')
res.encoding = 'utf-8'
html = res.text
soup = BeautifulSoup(html,'html.parser')#解析文档
weathers = soup.find(id="7d",class_="c7d").find('ul',class_="t clearfix").find_all('li')

result = {}
for weather in weathers:
weather_date = weather.find('h1')
weather_wea = (weather.find('p',class_='wea'))
weather_tem = (weather.find('p',class_= 'tem'))

result['日期'] = weather_date.text
result['天气'] = weather_wea.text
result['温度'] = weather_tem.text[1:-1]


print(result)
# print('日期:',result['日期'])

return result
print(jj_weather())