爬取城市和最低氣溫。
# 爬取全國所有城市名稱和最低氣溫
# http://www.weather.com.cn/textFC/hb.shtml
import requests
from bs4 import BeautifulSoup
def parse_page(url):
response = requests.get(url)
text = response.content.decode('utf-8') # 解決亂碼
soup = BeautifulSoup(text,'html5lib')
# 網頁解析
conMidtab = soup.find('div',class_= 'conMidtab')
# table
tables = conMidtab.find_all('table')
# tr
for table in tables:
trs = table.find_all("tr")[2:]
for tr in trs:
city_td = tr.find_all('td')[-8]
city = list(city_td.stripped_strings)[0] # 打印子孫節點的文本
temp_td = tr.find_all('td')[-2]
temp = list(temp_td.stripped_strings)[0]
print(city,temp)
def main():
areas = ['hb','hd','gat','hz']
for area in areas:
url = 'http://www.weather.com.cn/textFC/{}.shtml'.format(area)
parse_page(url)
if __name__ == '__main__':
main()