• 大小: 318KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-04
  • 语言: Python
  • 标签:

资源简介

全国122个城市历史空气质量数据及代码,数据及python代码,供参考;全国122个城市历史空气质量数据及代码,数据及python代码,供参考

资源截图

代码片段和文件信息

#encoding=utf-8
from bs4 import BeautifulSoup
import requests

base_url = ‘https://www.aqistudy.cn/historydata/daydata.php?city=‘
str_city = ‘北京‘

def get_month_set():
    month_set = list()
    for i in range(7 10):
        month_set.append((‘2015-0%s‘ % i))
    for i in range(10 13):
        month_set.append((‘2015-%s‘ % i))
    for i in range(1 10):
        month_set.append((‘2016-0%s‘ % i))
    month_set.append((‘2016-%s‘ % 10))
    month_set.append((‘2016-%s‘ % 11))
    return month_set

def get_city_set():
    str_file = r‘city.txt‘
    fp = open(str_file‘rb‘)
    city_set = list()
    for line in fp.readlines():
        city_set.append(str(line.strip()encoding=‘utf-8‘))
    return city_set

month_set = get_month_set()
city_set = get_city_set()

for city in city_set:
    file_name = city + ‘.csv‘
    fp = open(‘aqi/‘ + file_name ‘w‘)
    for i in range(len(month_set)):
        str_month = month_set[i]
        weburl = (‘%s%s&month=%s‘ % (base_urlcitystr_month))
        response = requests.get(weburl).content
        soup = BeautifulSoup(response‘html.parser‘from_encoding=‘utf-8‘)
        result = soup.find_all(‘td‘attrs={‘align‘:‘center‘}recursive=True)

        for j in range(0len(result) - 1111):
            tag_date = result[j]
            tag_aqi = result[j + 1]
            record_day = tag_date.get_text().strip()
            record_aqi = tag_aqi.get_text().strip()
            fp.write((‘%s%s\n‘ % (record_dayrecord_aqi)))

        print(‘%d---%s%s---DONE‘ % (city_set.index(city) city str_month))
    fp.close()

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-02-23 08:16  aqistudy-master\
     文件        1236  2017-02-23 08:16  aqistudy-master\README.md
     目录           0  2017-02-23 08:16  aqistudy-master\aqi\
     文件      206744  2017-02-23 08:16  aqistudy-master\aqi\alldata.csv
     文件        7266  2017-02-23 08:16  aqistudy-master\aqi\三亚.csv
     文件        7269  2017-02-23 08:16  aqistudy-master\aqi\三明.csv
     文件        7346  2017-02-23 08:16  aqistudy-master\aqi\上海.csv
     文件        7283  2017-02-23 08:16  aqistudy-master\aqi\上饶.csv
     文件        7279  2017-02-23 08:16  aqistudy-master\aqi\东莞.csv
     文件        7474  2017-02-23 08:16  aqistudy-master\aqi\东营.csv
     文件        7278  2017-02-23 08:16  aqistudy-master\aqi\中山.csv
     文件        7281  2017-02-23 08:16  aqistudy-master\aqi\丽水.csv
     文件        7361  2017-02-23 08:16  aqistudy-master\aqi\乐山.csv
     文件        7298  2017-02-23 08:16  aqistudy-master\aqi\佛山.csv
     文件        7525  2017-02-23 08:16  aqistudy-master\aqi\保定.csv
     文件        7394  2017-02-23 08:16  aqistudy-master\aqi\信阳.csv
     文件        7345  2017-02-23 08:16  aqistudy-master\aqi\六安.csv
     文件        7468  2017-02-23 08:16  aqistudy-master\aqi\北京.csv
     文件        7356  2017-02-23 08:16  aqistudy-master\aqi\十堰.csv
     文件        7361  2017-02-23 08:16  aqistudy-master\aqi\南京.csv
     文件        7290  2017-02-23 08:16  aqistudy-master\aqi\南宁.csv
     文件        7268  2017-02-23 08:16  aqistudy-master\aqi\南平.csv
     文件        7332  2017-02-23 08:16  aqistudy-master\aqi\南昌.csv
     文件        7358  2017-02-23 08:16  aqistudy-master\aqi\南通.csv
     文件        7269  2017-02-23 08:16  aqistudy-master\aqi\厦门.csv
     文件        7289  2017-02-23 08:16  aqistudy-master\aqi\台州.csv
     文件        7390  2017-02-23 08:16  aqistudy-master\aqi\合肥.csv
     文件        7331  2017-02-23 08:16  aqistudy-master\aqi\咸宁.csv
     文件        7456  2017-02-23 08:16  aqistudy-master\aqi\咸阳.csv
     文件        7383  2017-02-23 08:16  aqistudy-master\aqi\哈尔滨.csv
     文件        7331  2017-02-23 08:16  aqistudy-master\aqi\嘉兴.csv
............此处省略97个文件信息

评论

共有 条评论