爬取中國天氣網

爬取城市和最低氣溫。

# 爬取全國所有城市名稱和最低氣溫

# 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()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章