中国城市空气质量查询及可视化bs4+wxpython

运行要求:

  1. py3
  2. wxPython
  3. bs4
  4. matplotlib

运行说明:

  1. python demo.py
  2. textctrl文本框内可输入查询城市名全拼(不可以有空格)、年、月,城市名不限listctrl中所列,单击query查询
  3. 双击listctrl中所列行,可查询当月该城市空气质量
  4. 数据可视化结果显示在界面右侧

运行界面截图:


源码:

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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值