运行要求:
- py3
- wxPython
- bs4
- matplotlib
运行说明:
- python demo.py
- textctrl文本框内可输入查询城市名全拼(不可以有空格)、年、月,城市名不限listctrl中所列,单击query查询
- 双击listctrl中所列行,可查询当月该城市空气质量
- 数据可视化结果显示在界面右侧
运行界面截图:![](https://i-blog.csdnimg.cn/blog_migrate/65905b2f6a74024262d9573bfd0d1e59.png)
源码:
demo.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Author: XiaoY --<zhaoyin@octvision.com>
Purpose:
Created: 2018/11/25
"""
import wx
import wx_frame
if __name__ == '__main__':
app = wx.App(False)
top = wx_frame.WxFrameAirQuality("Query Air Quality")
top.Show(True)
app.MainLoop()
web_crawl.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Author: XiaoY --<zhaoyin@octvision.com>
Purpose:
Created: 2018/11/26
"""
import time
import requests
import bs4
dictHeaders = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
# ---------------------------------------------------------------------
def retrieveCurrentAirQuality(lstCities):
""""""
strLocalTime = time.strftime("%Y-%m-%d", time.localtime())
strYear, strMonth, strDay = strLocalTime.split('-')
lstAirQuality = []
for idxCity in range(len(lstCities)):
strUrl = "http://www.tianqihoubao.com/aqi/{0}-{1}{2}.html".format(
lstCities[idxCity], strYear, strMonth)
response = requests.get(url=strUrl, headers=dictHeaders)
soup = bs4.BeautifulSoup(response.text, "html.parser")
lstTableRows = soup.find_all("tr")
lstTableData = lstTableRows[-1].find_all("td")
strDate = lstTableData[0].get_text().strip()
strGrade = lstTableData[1].get_text().strip()
strAQI = lstTableData[2].get_text().strip()
strRanking = lstTableData[3].get_text().strip()
strPM2p5 = lstTableData[4].get_text().strip()
strPM10 = lstTableData[5].get_text().strip()
strSo2 = lstTableData[6].get_text().strip()
strNo2 = lstTableData[7].get_text().strip()
strCo = lstTableData[8].get_text().strip()
strO3 = lstTableData[9].get_text().strip()
lstAirQuality.append([lstCities[idxCity], strAQI, strRanking, strPM2p5,
strPM10, strSo2, strNo2, strCo, strO3])
return lstAirQuality
# ---------------------------------------------------------------------
def retrieveQuoteHist(strCity, strYear, strMonth):
""""""
lstAirQuality = []
strUrl = "http://www.tianqiho