python通過呼叫百度天氣API介面獲取天氣資訊

2021-10-24 22:23:55 字數 4717 閱讀 1660

# -

*- coding: utf-8-

*-# author: herman_tang

import requests

import re

import json

import csv

import sys

import os

apikey =

'eiaoyez8eo9mpu3wkaz0wda64xagu2fu'

city =

input

("請輸入要查詢的城市名: "

)file1 = r'e:\python檔案\tang01\district_id.csv'

def is_chinese()

: count1 =

len(city)

count =

0while count < count1:

if'\u4e00'

<= city[count]

<=

'\u9fff'

: number =

'true'

else

: number =

'false'

break

count +=1

return number

def distr_id()

:

with open

(file1,

'r', encoding=

'utf-8'

) as filea:

lines = csv.

reader

(filea)

for i in lines:

if i[5]

== city:

district_id = i[1]

return district_id

def get_info

(data_type: str =

'all'):

""" 這裡傳遞的產生來data_type一共以下幾種型別:

1、all 所有資訊

2、now 實時資料

3、alert 氣象預警

4、index 生活指數

5、fc 預報資料

6、fc_hour 未來24小時小時預報(高階字段)

預設為all,返回所有的資訊

"""try

:

district_id =

distr_id()

except:

return

"您輸入的城市名{}不正確,請重新輸入.........."

.format

(city)

url =

"".format

(district_id, data_type, apikey)

payload =

headers =

response = requests.

request

("get"

, url, headers=headers, data = payload)

info = json.

dumps

(json.

loads

(response.text)

, ensure_ascii=false)

weather_info =

eval

(info)

return weather_info

def p_info()

:'''

這裡是呼叫get_info函式,解析獲取到的資訊,將資訊進行轉換

'''weather_info =

get_info()

['result'

] now = weather_info[

'now'

] futrue = weather_info[

'forecasts'

] country = weather_info[

'location'][

'country'

] province = weather_info[

'location'][

'province'

] city = weather_info[

'location'][

'city'

] county = weather_info[

'location'][

'name'

] text = now[

'text'

] temp = now[

'temp'

] feels_like = now[

'feels_like'

] rh = now[

'rh'

] wind_class = now[

'wind_class'

] wind_dir = now[

'wind_dir'

]#now = '實時資料:'

''' 這裡是獲取當天天氣資訊

'''if county == city[:-

1]:

print (

'您要查詢天氣的位置為: {}{}{}市'

.format

(country, province, county)

)else

: print (

'您要查詢天氣的位置為: {}{}{}{}市'

.format

(country, province, city, county)

) text_info =

['當前天氣為:{}'

.format

(text)

,'當前為氣溫: {}攝氏度'

.format

(temp)

,'當前濕度為: {}'

.format

(rh)

,'當前體感溫度為: {}'

.format

(feels_like)

,'當前風向為: {}'

.format

(wind_dir)

,'當前風力為: {}級'

.format

(wind_class)

]for i in text_info:

print (i)

""" 下面是解析未來幾天的天氣資訊

"""for i in futrue:

text_day = i[

'text_day'

] text_night = i[

'text_night'

] high = i[

'high'

] low = i[

'low'

] wc_day = i[

'wc_day'

] wd_day = i[

'wd_day'

] wc_night = i[

'wc_night'

] wd_night = i[

'wd_night'

] date = i[

'date'

] week = i[

'week'

] futrue_info =

['日期:{}'

.format

(date)

, week,

'白天天氣: {}'

.format

(text_day)

,'晚上天氣: {}'

.format

(text_night)

,'最高氣溫: {}攝氏度'

.format

(high)

,'最低氣溫: {}攝氏度'

.format

(low)

,'白天風力: {}'

.format

(wc_day)

,'白天風向: {}'

.format

(wd_day)

,'晚上風力: {}'

.format

(wc_night)

,'晚上風向: {}'

.format

(wd_night)

]for i in futrue_info:

print (i)

return

"我喜歡緊緊抱住你的那一刻,就像得到了全世界!"

def select()

: zh =

is_chinese()

if zh ==

'true'

:try

:return

p_info()

except:

return

"nameerror: 您輸入的城市名字有誤......."

else

:return

'nameerror: 請輸入中文名.............'

if __name__ ==

"__main__"

: s1 =

select()

print (s1)

#p_info()

Python呼叫百度API之天氣查詢

標籤 空格分隔 python api coding utf 8 python 2.7.9 通過拼音訪問城市天氣 import sys,urllib,urllib2,json url city raw input 輸入你想查詢城市的名稱拼音 完整api訪問介面 url url city req url...

百度天氣預報

根據經緯度 城市名查詢天氣的結果 引數型別 引數名稱 是否必須 具體描述 string location true 輸入城市名或經緯度,城市名稱如 北京或者131,經緯度格式為lng,lat座標如 location 116.305145,39.982368 全國值為all,返回省會城市自治區,港澳台...

php呼叫百度天氣介面,微信實時檢視天氣

header content type text charset utf8 define token weixin wechatobj new wechatcallbackapitest if isset get echostr else class wechatcallbackapitest pr...