1. 程式人生 > >Python一個簡單的抓取天氣資料的API介面

Python一個簡單的抓取天氣資料的API介面

前提安裝:Python3

安裝第三方庫:pip3 install urllib3; pip3 install BeautifulSoup4; pip3 install bottle

import urllib.request  

import json
from bs4 import BeautifulSoup 
from bottle import route, run, request




'''
獲取所有城市的空氣質量資料
''' 
@route('/getCitysAirQualityData')
def getCitysAirQualityData():


    #網址  
    url = "http://www.pm25.com/rank.html"  
  
    #請求  
    request = urllib.request.Request(url)  
  
    #爬取結果  
    response = urllib.request.urlopen(request)  
  
    data = response.read()  
  
    #設定解碼方式  
    data = data.decode('utf-8')  




    #列印結果
    #用BeautifulSoup解析資料  python3 必須傳入引數二'html.parser' 得到一個物件,接下來獲取物件的相關屬性
    html=BeautifulSoup(data,'html.parser')
    dataArray = html.find_all('li', 'pj_area_data_item')


    cityAirQualityArray = []
    # 獲取想要資料的字典格式
    for ulData in dataArray:
        cityName = ulData.find('a', 'pjadt_location')
        pm25Value = ulData.find('span', 'pjadt_pm25')
        aqiValue = ulData.find('span', 'pjadt_aqi')


        # 去除指定字串
        pm25 = '' + str(pm25Value.get_text())
        pm25 = pm25.replace('μg/m³', '')
        # 去掉左右兩邊空格
        pm25 = pm25.strip()


# 設定字典
        cityAndPm25Dict = {}
        cityAndPm25Dict.setdefault('city', cityName.get_text())
        cityAndPm25Dict.setdefault('pm25', pm25)
        cityAndPm25Dict.setdefault('aqi', aqiValue.get_text())
        
        # 新增到陣列中
            # 判斷是否資料已存在
        isExist = False
        #pdb.set_trace()  # 除錯
        for cityData in cityAirQualityArray:
            myCity = cityData['city']
            if myCity == cityName.get_text():
                isExist = True


        if isExist != True:
            cityAirQualityArray.append(cityAndPm25Dict)


    dataString = json.dumps(cityAirQualityArray)

    return "{\"status\":\"101\",\"msg\":\"操作成功\",\"data\":" + dataString + "}"

run(host='0.0.0.0', port=8080, debug=True)