1. python怎麼在天氣數據中篩選最高氣溫大於10度的日期
python在天氣數據中篩選最高氣溫大於10度的日期步驟如下。
1、在命令行中直接使用pip進行模塊安裝。
2、利用select語句找到網頁中天氣數據所在的div即可。
2. 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
3. Python爬取天氣信息的時候出現了錯誤提示
應該是編碼的問題,建議city這個字典的key使用unicode,即u'鄭州'。
另外對輸入值做編碼轉換(str.encode('unicode')),編碼是一個頭疼的問題,你可以print下key和你輸入的內容是不是一個東西。
最後,建議使用字典的get方法來取值,即city.get(str)。
如果是自己用的代碼或者城市不多,可以使用代碼來替代,例如0371
4. python如何提取網頁天氣信息
bs4是可以提取的,因為你這一段裡面出現的文字都是你要的,不存在剔除的考慮。
網頁解析:要麼使用bs4、要麼使用bs4+re(正則),或者你可以使用以下PyQuery,這個也是用在網頁爬蟲解析頁面的模塊。
如果還是琢磨不出來,你把你這整個的html源碼發上來,我搞搞,現在只看一段很難幫你
5. 求助:用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格式數據;第三個返回是一個頁面,需要自己從頁面里提取想要的信息。
6. 如何使用python利用api獲取天氣預報
這個和用不用python沒啥關系,是數據來源的問題。調用淘寶API,使用 api相關介面獲得你想要的內容,我 記得api中有相關的介面,你可以看一下介面的說明。用python做爬蟲來進行頁面數據的獲取。希望能幫到你。