__author__ = '田明博'
__date__ = '2019/10/9 21:23'
'''獲取所有城市的天氣預報,按最低溫度排名
'''import requests
import operator
from bs4 import beautifulsoup
import matplotlib.pyplot as plt
def get_page(link):
'''獲取每乙個頁面的源**,並分析
:param link:每乙個頁面的鏈結
:return:
'''all_temp =
url = link
resp = requests.get(url)
resp.encoding = 'utf-8'
soup = beautifulsoup(resp.text, 'html5lib') # html5lib解析器,解析速度沒有lxml快
conmidtab = soup.find('div', attrs=) #第乙個,每週七天,提取當天的即可
tables = conmidtab.find_all('table')
for table in tables:
trs = table.find_all('tr')[2:] # 獲取資料
for index, tr in enumerate(trs):
one_info = {} # 一條記錄
tds = tr.find_all('td') # 找到所有的資訊td
city_td = tds[0] # 找到city所在的**
if index == 0:
city_td = tds[1]
city = list(city_td.stripped_strings)[0] # city名字
temp = tds[-2] # 溫度
temp = int(list(temp.stripped_strings)[0])
one_info['city'] = city
one_info['temp'] = temp
return all_temp
def show_charset(min_temp_citys):
'''展示圖表
:param min_temp_citys:
:return:
'''# print(min_temp_citys)
x =
y =
# 解析獲取的前十資料(字典格式)
for i in min_temp_citys:
plt.rcparams['font.sans-serif'] = ['simhei'] # 用來正常顯示中文標籤
plt.rcparams['axes.unicode_minus'] = false # 用來正常顯示負號
plt.title('全國溫度最低的前十城市')
plt.xlabel('城市') # 橫座標
plt.ylabel('溫度℃') # 縱座標
plt.bar(x, y) # 繪製柱狀圖
plt.show()
def main():
all_infos =
# 各地區鏈結
links = ['',
'','',
'','',
'','', ]
for link in links:
all = get_page(link)
all_infos = all_infos + all # 用於拼接列表
# print(all_infos)
min_temp_ten = sorted(all_infos, key=operator.itemgetter('temp'))[:10]
print(min_temp_ten)
show_charset(min_temp_ten)
if __name__ == '__main__':
main()
採用matplotlib庫繪製。
python爬蟲之墨跡天氣
import requests from lxml.html import etree import json import time 匯入模組 class mojiweather def city name self 定義乙個輸入城市名稱的函式 cityname str input 輸入城市名稱 ...
python網頁爬蟲之天氣查詢
這是用python寫的天氣查詢 可用。這裡用的是和風天氣的介面 參考 我用的城市資料 將城市資料存放本地 jsons import pickle pickle file open city data.pkl wb pickle.dump jsons,pickle file pickle file.c...
爬天氣的乙個小爬蟲
使用bs,requests寫的 coding utf 8 import requests import re import argparse from bs4 import beautifulsoup parser argparse.argumentparser parser.add argumen...