"""
time:2019.9.25
people:舊紙
"""
import requests
from bs4 import BeautifulSoup
ALL_DATA = [] ##列表裝獲取的地點,天氣,最高氣溫,最低氣溫
def parse_page(url): ##定義一個函數,分析頁面列表
headers = { ##獲取頁面頭部信息
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
'Referer':'http://www.weather.com.cn/textFC/hn.shtml'
}
response = requests.get(url,headers = headers) ##獲取網頁
text = response.content.decode("utf-8") ##編譯成網頁賦值給text
soup = BeautifulSoup(text,'html5lib')
conMidtab = soup.find('div',class_='conMidtab') ##獲取網頁中‘div’,class = ‘conMidtab’的元素conMidtab
tables = conMidtab.find_all('table') ##獲取conMidtab裏面的table元素
for table in tables:
trs = table.find_all('tr')[2:]
for index,tr in enumerate(trs):
tds = tr.find_all('td')
city_td = tds[0] ###獲取城市
if index == 0:
city_td = tds[1]
city = list(city_td.stripped_strings)[0] ###獲取城市
temp_td = tds[-2]
##獲取最低溫度
temp = list(temp_td.stripped_strings)[0] #獲取最低溫度
weather_td = tds[4] ##獲取天氣
if index == 0:
weather_td = tds[5]
weather = list(weather_td.stripped_strings)[0] ##獲取天氣
max_temp_td = tds[3] ##獲取最高氣溫
if index == 0:
max_temp_td = tds[4]
max_temp = list(max_temp_td.stripped_strings)[0]
#ALL_DATA.append({'城市:':city,'天氣:':weather,'最高氣溫':max_temp,'最低氣溫':temp})
# print(ALL_DATA)
print({'城市:':city,'天氣:':weather,'最高氣溫':max_temp,'最低氣溫':temp})
def main():
region = ['hb','db','hd','hz','hn','xb','xn'] #,'gat'
for x in region:
url = 'http://www.weather.com.cn/textFC/{}.shtml'.format(x)
parse_page(url)
# url = 'http://www.weather.com.cn/textFC/gat.shtml'
# parse_page(url)
#分析數據
##提取前面十個最冷的氣溫(排序)
if __name__ == '__main__':
main()
效果圖