Python 爬取天氣和星座(自學)

2021-10-18 04:20:11 字數 2321 閱讀 2333

import requests

import itchat

def get_sentence(

): jinshanapi =

''sentence = requests.get(jinshanapi)

return sentence.json(

)def get_weather_forecast(

): apikey =

'ffa2e199264d8575ebb6a165a272852c'

url =

weather_forecast = requests.get(url)

return weather_forecast.json(

)if __name__ ==

'__main__'

: sentence = get_sentence(

)

datetime = sentence[

'datetime'

] name = sentence[

'name'

] qfriend = sentence[

'qfriend'

] color = sentence[

'color'

] health = sentence[

'health'

] love = sentence[

'love'

] work = sentence[

'work'

] money = sentence[

'money'

] number = sentence[

'number'

] summary = sentence[

'summary'

] all = sentence[

'all'

] print(

'-----------今日星座-----------'

) print(

' 今日:%s\n 星座:%s\t 上公升星座:%s\n 顏色:%s\t 健康:%s\n 愛情:%s\t 事業:%s\n 金錢:%s\t 幸運數字:%s\n 今日總結:%s\n 總值:%s\n'%(datetime, name, qfriend, color , health, love, work ,money,number, summary, all))

weatherforecast = get_weather_forecast(

) print(

'-----------今日天氣-----------'

)# print(weatherforecast)

city = weatherforecast[

'city'

] week = weatherforecast[

'week'

] wea = weatherforecast[

'wea'

] tem = weatherforecast[

'tem'

] tem1 = weatherforecast[

'tem1'

] tem2 = weatherforecast[

'tem2'

] win = weatherforecast[

'win'

] win_meter = weatherforecast[

'win_meter'

] humidity = weatherforecast[

'humidity'

] visibility = weatherforecast[

'visibility'

] air_pm25 = weatherforecast[

'air_pm25'

] air_level = weatherforecast[

'air_level'

] air_tips = weatherforecast[

'air_tips'

] print(

' 您當前所在地:中國·%s\n 日期:%s\t 天氣:%s\t 溫度:%s\n 最高溫度:%s\t 最低溫度:%s\n 風:%s\t \t風速:%s\t 濕度:%s\n 能見度:%s\t 空氣指數:%s\t 汙染程度:%s\n 建議:%s'%

(city, week, wea, tem , tem1, tem2, win ,win_meter,humidity,visibility,air_pm25,air_level,air_tips))

python 爬取天氣

準備工作做好了,接下來就是 了 用py爬天氣資訊,需要使用兩個模組,分別是urllib2 獲取資料 和json 解析資料 coding utf 8 import urllib2 import json from city import city cityname raw input 你想查哪個城市的...

Python 爬取天氣資訊

第一次python部落格,僅作紀念。import requests import re from bs4 import beautifulsoup requests庫從網上獲取資源,re bs4 庫,用來提取需要的資訊。開啟要爬取的 右擊檢視其源 找到感興趣的內容,如下 2020年01月12日 星期...

使用Python 爬取歷史天氣

最近做個簡單小專案需要一些天氣資料值,所以想去網上爬取一些。下面是我的爬取案例。使用的 是2345天氣網。2.xpath表示式 3.取文字 import requests from lxml import etree defget table url headers 請求資料 並獲得返回的json資...