def getday(): url = 'http://datacenter.mee.gov.cn/aqiweb2/' r = requests.get(url) r.encoding = 'gbk' soup = BeautifulSoup(r.text, 'html.parser') tb = soup.find(id="legend_02_table").find_all("tr") listaqi = [] for i in tb: aqi = [] riqi = re.findall(r"[(](.*?)[)]", i.get("onclick")) time = re.findall(r"\'(.+?)\'", riqi[0]) aqi.append(time[1].replace('年', '-').replace('月', '-').replace( '日', ' ').replace('时', ':00:00')) print(time[1].replace('年', '-').replace('月', '-').replace('日', ' ').replace( '时', ':00:00')) td = i.find_all("td") for d in td: for dd in d: print(dd.strip()) aqi.append(dd.strip()) listaqi.append(tuple(aqi)) mysqlHelp.addcrows( "INSERT INTO quanguoallcitys (DataTime ,CityName ,AQI ,Type, MaxPoll ) VALUES(%s,%s,%s,%s,%s)", listaqi) pass
def getarea(Pointerlist): listRegionaqi = [] for point in Pointerlist: City = point.find('City').text Region = point.find('Region').text Name = point.find('Name').text DataTime = (('%d' + '-' + point.find('DataTime').text) % datetime.datetime.now().year).replace('/', '-') AQI = point.find('AQI').text Level = point.find('Level').text LevelIndex = point.find('LevelIndex').text MaxPoll = point.find('MaxPoll').text Intro = point.find('Intro').text Tips = point.find('Tips').text CLng = point.find('CLng').text CLat = point.find('CLat').text p=getpoll(point.find('Polls')) PM25 = p['Pm25'] PM10 = p['PM10'] SO2 = p['SO2'] CO = p['CO'] NO2 = p['NO2'] O38H = p['O3-8H'] O31H = p['O3-1H'] sql = (City, Region, Name, DataTime, AQI, Level, LevelIndex, MaxPoll, Intro, Tips, CLng, CLat, PM25, PM10, SO2, CO, NO2, O38H, O31H ) listRegionaqi.append(sql) # str = "INSERT INTO Region (CityName ,Region,Site,DataTime ,AQI ,Level ,LevelIndex ,MaxPoll ,Intro ,Tips, CLng,CLat,PM25 , PM10 , SO2, CO, NO2 , O38H ,O31H ) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);" # mysqlHelp.addcrow(str, sql) str = "INSERT INTO Region (CityName ,Region,Site,DataTime ,AQI ,Level ,LevelIndex ,MaxPoll ,Intro ,Tips, CLng,CLat,PM25 , PM10 , SO2, CO, NO2 , O38H ,O31H ) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);" mysqlHelp.addcrows(str, listRegionaqi)
def getpm25(): rpm25 = requests.post(aqiurl, data=pm25params, headers=headerss) listpm25 = [] for i in rpm25.json(): name = i['name'] lastDataTime = (datetime.datetime.now()-datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:00:00") lat = i['lat'] lng = i['lng'] paraValue = i['paraValue'] paraName = i['paraName'] sql = (lastDataTime, name, paraValue, lat, lng) print(sql) listpm25.append(sql) mysqlHelp.addcrows( "INSERT INTO xthourmp25 (DataTime ,Name ,PM25 ,lat ,lng ) VALUES(%s,%s,%s,%s,%s)",listpm25) pass
def getSO2(): rso2 = requests.post(aqiurl, data=so2params, headers=headerss) listso2 = [] for i in rso2.json(): name = i['name'] lastDataTime = (datetime.datetime.now()-datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:00:00") lat = i['lat'] lng = i['lng'] paraValue = i['paraValue'] paraName = i['paraName'] if(lastDataTime ==""): time ="" else: time = lastDataTime.replace('/', '-') pass sql = (time, name, paraValue, lat, lng) print(sql) listso2.append(sql) mysqlHelp.addcrows("INSERT INTO xthourmso2 (DataTime ,Name ,SO2 ,lat ,lng ) VALUES(%s,%s,%s,%s,%s)", listso2) pass