python 爬取中國天氣網 資料視覺化

2021-09-26 00:24:18 字數 1560 閱讀 9087

需要安裝的庫:requests,bs4,pyecharts[版本0.1.9.4],lxml,html5lib

**複製貼上即可用,2023年8月9日測試通過

爬取全國城市的最高溫度,以及匯出張柱狀圖:

import requests

from bs4 import beautifulsoup

from pyecharts import bar

data =

def parse_page(url):

headers=

response = requests.get(url,headers=headers)

text = response.content.decode('utf-8')

soup = beautifulsoup(text,"html5lib")

conmidtab=soup.find('div',class_="conmidtab")

tables = conmidtab.find_all('table')

for table in tables:

trs = table.find_all('tr')[2:]

for tr in trs :

city = tr.find_all('td')[-8]

city_name = list(city.stripped_strings)[0]

temp = tr.find_all('td')[-5]

max_temp=list(temp.stripped_strings)[0]

def main():

baseurl = ""

areas = ['hb','db','hz','hd','hn','xb','xn','gat']

for area in areas:

url = baseurl.format(area)

parse_page(url)

# 分析資料

# 排序

data.sort(key=lambda data:data['temp'],reverse=true)

print(data)

data = data[:10]

chart = bar("中國城市最高氣溫排行榜")

chart.add("溫度",

list(map(lambda data:data['city'],data)),

list(map(lambda data:data['temp'],data))

)chart.render('temperature.html')

if __name__=='__main__':

main()

執行截圖:

自動匯出視覺化資料:

Python爬取中國天氣網天氣資料

由於一些需要,想要獲取今天的天氣資料,於是又撿起了python寫了個爬蟲用來獲取中國天氣網上的氣象資料。由於我需要的資料比較簡單,因為我只需要北京地區當天的溫度 最低溫度和最高溫度 和天氣,因此 部分比較簡單,下面就來講講這個爬取的過程。第一步 網頁分析 要進行爬蟲設計,首先得分析網頁的請求過程。首...

Python爬取中國天氣網指定城市天氣

功能 完整 import pandas as pd import requests import re 建立乙個字典儲存中國天氣網城市 def createcitycode fh r text 中國天氣網城市 csv data pd.read csv fh,engine python data da...

Python 爬取中國天氣網天氣並通過郵箱定時傳送

獲取天氣資訊指令碼如下,usr bin python3 coding utf 8 import re import requests from bs4 import beautifulsoup import io import sys r requests.get timeout 30 r.rais...