日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬去中国天气网_python爬取中国天气网并展示最低温度

發(fā)布時間:2025/3/11 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬去中国天气网_python爬取中国天气网并展示最低温度 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

import requests

from bs4 import BeautifulSoup

import lxml

import json

from echarts import Echart,Bar,Axis

import time

#城市與溫度的集合

weather_list = []

#城市集合

city_list = []

#溫蒂集合

lowest_list =[]

#獲取溫度

def getTemperature(url):

result = requests.get(url)

#print result.content

soup = BeautifulSoup(result.content,'lxml')

div_conMidTab = soup.find("div",attrs={'class':'conMidtab'})

div_conMidTab2 = div_conMidTab.find_all("div",attrs={'class':'conMidtab2'})

for x in div_conMidTab2:

tr_list = x.find_all("tr")[2:]

provider =''

for index,tr in enumerate(tr_list):

td_list = tr.find_all('td')

if index ==0:

provider = td_list[0].text.replace('\n','')

city = td_list[1].text.replace('\n','')

#最高溫度

hight_temperature = td_list[4].text

#最低溫度

low_temperatrue = td_list[7].text

else:

city = td_list[0].text.replace('\n','')

# 最高溫度

hight_temperature = td_list[3].text

#最低溫度

low_temperatrue = td_list[6].text

print provider+' '+city+' '+low_temperatrue+'~'+hight_temperature

weather_list.append({

'city':provider+city,

'min':low_temperatrue

})

city_list.append(provider+city)

lowest_list.append(low_temperatrue)

print '-' * 40

def main():

result = requests.get('http://www.weather.com.cn/textFC/hb.shtml')

soup = BeautifulSoup(result.content,'lxml')

#獲取華東,華北...等地區(qū)對應的url港澳臺的數(shù)據(jù)格式不一樣,過濾掉

li_list = soup.find("ul",attrs={'class':'lq_contentboxTab2'}).find_all('li')[0:-1]

#遍歷獲取數(shù)據(jù)

for li in li_list:

url = 'http://www.weather.com.cn/'+li.find('a')['href']

getTemperature(url)

time.sleep(1)

#寫到本地文件

# line = json.dumps(weather_list,ensure_ascii=False)

# with open ('weather.json','w') as fp:

# fp.write(line.encode('utf-8'))

#讀取本地的json文件

# with open('weather.json', 'r') as fp:

# weather_list = json.load(fp,encoding='utf-8')

#將數(shù)據(jù)進行排序

sort_weather_list = sorted(weather_list,lambda x,y:cmp(int(x['min']),int(y['min'])))

#獲取前20名

TOP10_SORT_WEATHER_LIST = sort_weather_list[0:20]

TOP10_CITY = []

TOP10_MIN = []

#遍歷將城市和低溫放到新的集合中

for city_min in TOP10_SORT_WEATHER_LIST:

TOP10_CITY.append(city_min['city'])

TOP10_MIN.append(city_min['min'])

#創(chuàng)建表格

echart = Echart(u'低溫排名','溫度')

#柱狀圖

bar = Bar(u'最低溫度',TOP10_MIN)

#坐標

axis = Axis('category','bottom',data = TOP10_CITY)

echart.use(bar)

echart.use(axis)

echart.plot()

if __name__ == '__main__':

main()

總結(jié)

以上是生活随笔為你收集整理的python爬去中国天气网_python爬取中国天气网并展示最低温度的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。