-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAQI_5.0.py
41 lines (32 loc) · 1.07 KB
/
AQI_5.0.py
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
import requests
from bs4 import BeautifulSoup
def get_city_aqi():
"""
传入要访问的链接,获得链接所在网页的文本信息
解析网页,传入该函数所需的原网页的text文件,然后对该文件进行结构化解析
获取城市的aqi值
:param text:
:return:
"""
city_name = input('你想知道哪个城市的aqi,请输入拼音:')
url = 'http://pm25.in/' + city_name
r = requests.get(url,timeout=30)
soup = BeautifulSoup(r.text,'lxml')
div_list = soup.find_all('div', {'class': 'span1'})
#从div_list中再进行解析得到value值和caption描述信息
city_info = []
for i in range(8):
div_content = div_list[i]
value = div_content.find('div',{'class':'value'}).text.strip()
caption = div_content.find('div',{'class':'caption'}).text.strip()
city_info.append((caption,value))
return city_info
def main():
"""
主函数
:return:
"""
city_info = get_city_aqi()
print(city_info)
if __name__ == '__main__':
main()