如何用簡單的python代碼進行全國疫情實時更新

看到那些大佬花裏胡哨的一大串爬蟲代碼,如何簡單粗暴的得到全國疫情實時數據,這個代碼很簡單

# -- coding: utf-8 --
import requests
import json
import datetime
import time


def sleeptime(hour,min,sec):
    return hour*3600 + min*60 + sec;
second = sleeptime(0,30,0);



while (1):
    #疫情API
    json_text = requests.get("https://tianqiapi.com/api?version=epidemic&appid=88239943&appsecret=3oPsPEGO", params={'appid':'88239943' ,'appsecret':'3oPsPEGO'}).content
    # 取出疫情API json格式裏的部分數據
    data = json.loads(json_text)
    city=data['data']['list']
    city_t=str(city)
    city_o=city_t.replace('[',' ').replace(',','\n ')
    #area=data['data']['area']['cityName']
    print(city_o)
   # print(area)
    time.sleep(second)
    break

就20行不到【滑稽保命】,到這裏,就可以得到如圖的實時疫情
在這裏插入圖片描述

接下來把得到的實時消息通過微信接口發送消息提醒我

def get_access_token():
    """
    獲取微信全局接口的憑證(默認有效期倆個小時)
    如果不每天請求次數過多, 通過設置緩存即可
    """
    result = requests.get(
        url="https://api.weixin.qq.com/cgi-bin/token",
        params={
            "grant_type": "client_credential",
            "appid": "wx450e3952737015c9",
            "secret": "fa48630f7473044b648a5cd18d2e4b0e",
        }
    ).json()

    if result.get("access_token"):
        access_token = result.get('access_token')
    else:
        access_token = None
    return access_token

def sendmsg(openid,msg):

    access_token = get_access_token()




    response = requests.post(
        url="https://api.weixin.qq.com/cgi-bin/message/custom/send",
        params={
            'access_token': access_token
        },
        data=bytes(json.dumps(body, ensure_ascii=False), encoding='utf-8')
    )
    # 這裏可根據回執code進行判定是否發送成功(也可以根據code根據錯誤信息)
    result = response.json()
    print(result)


if __name__ == '__main__':
        sendmsg('weixin ID',send_data) #用戶ID  
        # sendmsg('oLV7xszi0ZHJfP1RhAYAnpJ638oQ',send_data)
    time.sleep(second);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章