當前位置:首頁 » 編程語言 » python天氣

python天氣

發布時間: 2022-02-06 00:46:23

python怎麼自動抓取網頁上每日天氣預報

使用到了urllib庫和bs4。bs4提供了專門針對html的解析功能,比用RE方便許多。
# coding : UTF-8import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )from bs4 import BeautifulSoupimport csvimport urllibdef get_html(url):
html = urllib.urlopen(url) return html.read()def get_data(html_text):
final = []
bs = BeautifulSoup(html_text, "html.parser")
body = bs.body
data = body.find('div', {'id': '7d'})
ul = data.find('ul')
li = ul.find_all('li') for day in li:
temp = []
date = day.find('h1').string
temp.append(date)
inf = day.find_all('p')
temp.append(inf[0].string,) if inf[1].find('span') is None:
temperature_highest = None
else:
temperature_highest = inf[1].find('span').string
temperature_highest = temperature_highest.replace('C', '')
temperature_lowest = inf[1].find('i').string
temperature_lowest = temperature_lowest.replace('C', '')
temp.append(temperature_highest)
temp.append(temperature_lowest)
final.append(temp) return finaldef write_data(data, name):
file_name = name with open(file_name, 'a') as f:
f_csv = csv.writer(f)
f_csv.writerows(data)if __name__ == '__main__':
html_doc = get_html('http://www.weather.com.cn/weather/101190401.shtml')
result = get_data(html_doc)
write_data(result, 'weather.csv') print

運行結果保存在csv文件中,如下:
28日(今天),小雨,,13℃29日(明天),小雨轉陰,15℃,12℃30日(後天),多雲,19℃,14℃31日(周一),小雨,16℃,14℃1日(周二),陰轉多雲,16℃,10℃2日(周三),多雲轉晴,17℃,10℃3日(周四),多雲轉晴,18℃,11℃1234567

㈡ Python獲取本土的天氣和任意城市的天氣怎麼解決

返回的數據是經過gzip壓縮的,如果你用urllib,需要先把獲取的二進制數據解壓,再解碼成字元串,包括編碼都幫你自動解決,不需要自己操心。

㈢ python求助 一段文字比如s=''' 天氣,真好。啊;(換行)天氣,真,不好 用replace

import re
print re.sub(r'[;。,\n]', ' ', string)

㈣ 如何利用python爬取某個地方1年的天氣

先要找到提供這個地方天氣信息的網站
然後用firefox之類的瀏覽器分析
之後用python按分析的結果來提取所需要的數據

㈤ 求助:用python獲取天氣預報

# 獲取溫度、濕度、風力等
WEATHER_URL_A = "http://www.weather.com.cn/data/sk/%s.html"

# 獲取天氣狀況、最大/小溫度等
WEATHER_URL_B = "http://www.weather.com.cn/data/cityinfo/%s.html"

# 獲取未來7天天氣數據
WEATHER_URL_C = "http://www.weather.com.cn/weather/%s.shtml"

URL里%s指城市對應的代碼。詳細參考:
http://www.cnblogs.com/toosuo/p/3868004.html
不過這篇文章里有的介面已經不能用了。
上面我給的三個URL里,前兩個直接返回json格式數據;第三個返回是一個頁面,需要自己從頁面里提取想要的信息。

㈥ python爬蟲完成了天氣數據的爬尋,然後可以做一些什麼變復雜或者變完整

再寫個發郵件模塊,根據爬取的天氣內容判斷,提醒用戶是否需要帶傘,適合穿什麼衣服出門!

㈦ 如何利用python給手機發天氣信息

分兩步走:

  1. 從天氣網站上抓取所要的天氣數據

  2. 調用第三方提供的簡訊介面發送所抓取的天氣數據

㈧ 如何使用python利用api獲取天氣預報

Python實現從網路API獲取天氣的方法
代碼如下:
__author__ = 'saint'
import os
import urllib.request
import urllib.parse
import json
class weather(object):
# 獲取城市代碼的uri
code_uri = "http://apistore..com/microservice/cityinfo?cityname="
# 獲取天氣信息的uri
weather_uri = "http://apistore..com/microservice/weather?cityid="
# 主處理邏輯
def mainHandle(self):
print("輸入你要查詢的天氣:")
city_name = input()
uri = self.code_uri + urllib.parse.quote(city_name)
ret = json.loads(urllib.request.urlopen(uri).read().decode("utf8"))
if ret['errNum'] != 0:
print(ret['retMsg'])
return False
else:
weather_uri = self.weather_uri + ret['retData']['cityCode']
data = json.loads(urllib.request.urlopen(weather_uri).read().decode("utf8"))
if data['errNum'] == 0:
ret_data = data['retData']
output = "城市名:" + city_name + "\r\n"
output += "更新時間:" + ret_data["date"] + " " + ret_data["time"] + "\r\n"
output += "天氣:" + ret_data["weather"] + " [" + ret_data["WD"] + ret_data["WS"] + "]\r\n"
output += "當前溫度:" + ret_data["temp"] + " (" + ret_data["h_tmp"] + " ---> " + ret_data["l_tmp"] + ")\r\n"
print(output)
return True
else:
print(data['errMsg'])
return False
if __name__ == "__main__":
weather = weather()
weather.mainHandle()

㈨ 求助,Python 查天氣代碼問題

weatherinfo=r.json() #在json後面加上括弧才能返回結果。否則只能返回函數地址。
以下python3通過:
import requests
ApiUrl="http://www.weather.com.cn/adat/cityinfo/101010100.html"
r=requests.get(ApiUrl)
weatherinfo=r.json()
print (weatherinfo["weatherinfo"]["ptime"])
print (weatherinfo["weatherinfo"]["temp2"])
>>>08:00
>>>5℃

㈩ 用python編寫的獲取天氣預報的代碼總是有錯誤,求解

weatherinfo=r.json() #在json後面加上括弧才能返回結果。否則只能返回函數地址。

以下python3通過:

importrequests
ApiUrl="http://www.weather.com.cn/adat/cityinfo/101010100.html"
r=requests.get(ApiUrl)
weatherinfo=r.json()
print(weatherinfo["weatherinfo"]["ptime"])
print(weatherinfo["weatherinfo"]["temp2"])

>>>08:00

>>>5℃

熱點內容
編譯成debug版本 發布:2024-03-29 09:06:55 瀏覽:884
wms伺服器地址 發布:2024-03-29 09:05:55 瀏覽:415
mep編程器 發布:2024-03-29 09:05:13 瀏覽:139
大小s我們一家訪問人 發布:2024-03-29 09:03:16 瀏覽:532
造物者編程 發布:2024-03-29 08:50:27 瀏覽:534
sql技能 發布:2024-03-29 08:50:23 瀏覽:56
希沃安卓下載安裝應用在哪裡 發布:2024-03-29 08:22:51 瀏覽:631
python和excel 發布:2024-03-29 07:47:03 瀏覽:861
postfix源碼下載 發布:2024-03-29 07:42:03 瀏覽:143
怎麼在電腦上玩手機伺服器 發布:2024-03-29 07:30:13 瀏覽:141