最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python3爬取各類天氣信息

 更新時(shí)間:2018年02月24日 09:20:14   作者:京局京段DF11G0025  
這篇文章主要為大家詳細(xì)介紹了python3爬取各類天氣信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本來(lái)是想從網(wǎng)上找找有沒(méi)有現(xiàn)成的爬取空氣質(zhì)量狀況和天氣情況的爬蟲(chóng)程序,結(jié)果找了一會(huì)兒感覺(jué)還是自己寫(xiě)一個(gè)吧。

主要是爬取北京包括北京周邊省會(huì)城市的空氣質(zhì)量數(shù)據(jù)和天氣數(shù)據(jù)。

過(guò)程中出現(xiàn)了一個(gè)錯(cuò)誤:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position 250。

原來(lái)發(fā)現(xiàn)是頁(yè)面的編碼是gbk,把語(yǔ)句改成data=urllib.request.urlopen(url).read().decode("gbk")就可以了。

然后我把爬到的數(shù)據(jù)寫(xiě)到文本文檔里了,往后可以導(dǎo)入到excel表中使用。

實(shí)驗(yàn)室的電腦不經(jīng)常開(kāi),然后就放到服務(wù)器上了,讓它自己慢慢一小時(shí)爬一次吧~哈哈哈~

后面有一次晚上出現(xiàn)了異常,因?yàn)闆](méi)加入異常處理,所以從零點(diǎn)到早上五點(diǎn)的數(shù)據(jù)都沒(méi)爬到。。。

(⊙﹏⊙)然后這次修改就加入了異常處理。如果出現(xiàn)URLError,就一分鐘后重試。

代碼:

#coding=utf-8 
#北京及周邊省會(huì)城市污染數(shù)據(jù)、天氣數(shù)據(jù)每小時(shí)監(jiān)測(cè)值爬蟲(chóng)程序 
import urllib.request 
import re 
import urllib.error 
import time 
#模擬成瀏覽器 
headers=("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36") 
opener = urllib.request.build_opener() 
opener.addheaders=[headers] 
#將opener安裝為全局 
urllib.request.install_opener(opener) 
def get_pm25_and_weather(city): 
 #首先執(zhí)行獲取空氣質(zhì)量數(shù)據(jù),返回?cái)?shù)據(jù)更新時(shí)間 
 data_time=getpm25(city) 
 #然后將獲取到的數(shù)據(jù)更新時(shí)間賦值給獲取天氣數(shù)據(jù)函數(shù)使用 
 getweather(city,data_time) 
def getpm25(city): 
 try: 
 #設(shè)置url地址 
 url="http://pm25.in/"+city 
 data=urllib.request.urlopen(url).read().decode("utf-8") 
 print("城市:"+city) 
 #構(gòu)建數(shù)據(jù)更新時(shí)間的表達(dá)式 
 data_time='<div class="live_data_time">\s{1,}<p>數(shù)據(jù)更新時(shí)間:(.*?)</p>' 
 #尋找出數(shù)據(jù)更新時(shí)間 
 datatime=re.compile(data_time, re.S).findall(data) 
 print("數(shù)據(jù)更新時(shí)間:"+datatime[0]) 
 #構(gòu)建數(shù)據(jù)收集的表達(dá)式 
 data_pm25 = '<div class="span1">\s{1,}<div class="value">\n\s{1,}(.*?)\s{1,}</div>' 
 data_o3='<div class="span1">\s{1,}<div class ="value">\n\s{1,}(.*?)\s{1,}</div>' 
 #尋找出所有的監(jiān)測(cè)值 
 pm25list = re.compile(data_pm25, re.S).findall(data) 
 o3list=re.compile(data_o3, re.S).findall(data) 
 #將臭氧每小時(shí)的值插入到原列表中 
 pm25list.append(o3list[0]) 
 print("AQI指數(shù),PM2.5,PM10,CO,NO2,SO2,O3:(單位:μg/m3,CO為mg/m3)") 
 print(pm25list) 
 #將獲取到的值寫(xiě)入文件中 
 writefiles_pm25(city,datatime,pm25list) 
 #返回?cái)?shù)據(jù)更新時(shí)間值 
 return datatime 
 except urllib.error.URLError as e: 
 print("出現(xiàn)URLERROR!一分鐘后重試……") 
 if hasattr(e,"code"): 
  print(e.code) 
 if hasattr(e,"reason"): 
  print(e.reason) 
 time.sleep(60) 
 #出現(xiàn)異常則過(guò)一段時(shí)間重新執(zhí)行此部分 
 getpm25(city) 
 except Exception as e: 
 print("出現(xiàn)EXCEPTION!十秒鐘后重試……") 
 print("Exception:"+str(e)) 
 time.sleep(10) 
 # 出現(xiàn)異常則過(guò)一段時(shí)間重新執(zhí)行此部分 
 getpm25(city) 
def writefiles_pm25(filename,datatime,pm25list): 
 #將獲取的數(shù)據(jù)寫(xiě)入文件中,數(shù)據(jù)分別為時(shí)間,AQI指數(shù),PM2.5,PM10,CO,NO2,SO2,O3。(單位:μg/m3,CO為mg/m3) 
 f = open("D:\Python\Python35\myweb\data_pm25\data_pm25_"+filename+".txt", "a") 
 f.write(datatime[0]) 
 f.write(",") 
 for pm25 in pm25list: 
 f.write(str(pm25)) 
 f.write(",") 
 f.write("\n") 
 print("該條空氣質(zhì)量數(shù)據(jù)已添加到文件中!") 
 f.close() 
def getweather(city,datatime): 
 try: 
 #構(gòu)建url 
 url="http://"+city+".tianqi.com/" 
 data=urllib.request.urlopen(url).read().decode("gbk") 
 #構(gòu)建數(shù)據(jù)收集的表達(dá)式 
 data_weather = '<li class="cDRed">(.*?)</li>' 
 data_wind='<li style="height:18px;overflow:hidden">(.*?)</li>' 
 data_temperature='<div id="rettemp"><strong>(.*?)°' 
 data_humidity='</strong><span>相對(duì)濕度:(.*?)</span>' 
 #尋找出所有的監(jiān)測(cè)值 
 weatherlist = re.compile(data_weather, re.S).findall(data) 
 windlist=re.compile(data_wind, re.S).findall(data) 
 temperaturelist = re.compile(data_temperature, re.S).findall(data) 
 humiditylist = re.compile(data_humidity, re.S).findall(data) 
 #將其他值插入到天氣列表中 
 weatherlist.append(windlist[0]) 
 weatherlist.append(temperaturelist[0]) 
 weatherlist.append(humiditylist[0]) 
 print("天氣狀況,風(fēng)向風(fēng)速,實(shí)時(shí)溫度,相對(duì)濕度:") 
 print(weatherlist) 
 #將獲取到的值寫(xiě)入文件中 
 writefiles_weather(city,datatime,weatherlist) 
 except urllib.error.URLError as e: 
 print("出現(xiàn)URLERROR!一分鐘后重試……") 
 if hasattr(e,"code"): 
  print(e.code) 
 if hasattr(e,"reason"): 
  print(e.reason) 
 time.sleep(60) 
 # 出現(xiàn)異常則過(guò)一段時(shí)間重新執(zhí)行此部分 
 getweather(city,datatime) 
 except Exception as e: 
 print("出現(xiàn)EXCEPTION!十秒鐘后重試……") 
 print("Exception:"+str(e)) 
 time.sleep(10) 
 # 出現(xiàn)異常則過(guò)一段時(shí)間重新執(zhí)行此部分 
 getweather(city, datatime) 
def writefiles_weather(filename,datatime,weatherlist): 
 #將獲取的數(shù)據(jù)寫(xiě)入文件中,數(shù)據(jù)分別為時(shí)間,天氣狀況,風(fēng)向風(fēng)速,實(shí)時(shí)溫度,相對(duì)濕度。 
 f = open("D:\Python\Python35\myweb\data_weather\data_weather_"+filename+".txt", "a") 
 f.write(datatime[0]) 
 f.write(",") 
 for weather in weatherlist: 
 f.write(str(weather)) 
 f.write(",") 
 f.write("\n") 
 print("該條天氣數(shù)據(jù)已添加到文件中!") 
 f.close() 
#退出循環(huán)可用Ctrl+C鍵 
while True: 
 print("開(kāi)始工作!") 
 get_pm25_and_weather("beijing") 
 get_pm25_and_weather("tianjin") 
 get_pm25_and_weather("shijiazhuang") 
 get_pm25_and_weather("taiyuan") 
 get_pm25_and_weather("jinan") 
 get_pm25_and_weather("shenyang") 
 get_pm25_and_weather("huhehaote") 
 get_pm25_and_weather("zhengzhou") 
 #每一小時(shí)執(zhí)行一次 
 print("休息中……") 
 print("\n") 
 time.sleep(3600) 

運(yùn)行狀態(tài)圖:

更多內(nèi)容請(qǐng)參考專題《python爬取功能匯總》進(jìn)行學(xué)習(xí)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

高阳县| 永胜县| 宜川县| 雅安市| 廊坊市| 莒南县| 威海市| 宁国市| 峨眉山市| 阜新市| 南昌市| 涟源市| 武宣县| 三台县| 青神县| 华坪县| 息烽县| 昌乐县| 莆田市| 平潭县| 河西区| 深水埗区| 东光县| 华亭县| 改则县| 西充县| 宝清县| 建平县| 江孜县| 洞口县| 商城县| 富蕴县| 鄂托克前旗| 汉川市| 大兴区| 乳源| 江油市| 五台县| 壤塘县| 门源| 隆回县|