1. 程式人生 > >Python爬蟲獲取最近七天天氣預報資訊

Python爬蟲獲取最近七天天氣預報資訊

主要用到python的requests庫和BeatifulSoup庫,程式碼如下:

#encoding:utf-8
import requests
import psycopg2
import datetime
import re
from bs4 import BeautifulSoup
from apscheduler.schedulers.background import BackgroundScheduler



#獲取最近七天天氣預報方法
def weatherPrediction():
    cityid = "57494"
    urls = ["https://tianqi.2345.com/today-"+cityid+".htm",
            "https://tianqi.2345.com/tomorrow-"+cityid+".htm",
            "https://tianqi.2345.com/third-"+cityid+".htm",
            "https://tianqi.2345.com/fourth-"+cityid+".htm",
            "https://tianqi.2345.com/fifth-"+cityid+".htm",
            "https://tianqi.2345.com/sixth-"+cityid+".htm",
            "https://tianqi.2345.com/seventh-"+cityid+".htm"]

    for url in urls:
        response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')

        nowDate = datetime.datetime.now()  # 當前日期
        weatherDate = ''

        if "today" in url:
            weatherDate = nowDate.strftime('%Y-%m-%d')
        elif "tomorrow" in url:
            weatherDate = (nowDate + datetime.timedelta(days=1)).strftime('%Y-%m-%d')  # 明天
        elif "third" in url:
            weatherDate = (nowDate + datetime.timedelta(days=2)).strftime('%Y-%m-%d')  # 第三天
        elif "fourth" in url:
            weatherDate = (nowDate + datetime.timedelta(days=3)).strftime('%Y-%m-%d')  # 第四天
        elif "fifth" in url:
            weatherDate = (nowDate + datetime.timedelta(days=4)).strftime('%Y-%m-%d')  # 第五天
        elif "sixth" in url:
            weatherDate = (nowDate + datetime.timedelta(days=5)).strftime('%Y-%m-%d')  # 第六天
        elif "seventh" in url:
            weatherDate = (nowDate + datetime.timedelta(days=6)).strftime('%Y-%m-%d')  # 第七天

        # -----------------------------------------天氣預報基本資訊開始-----------------------------------------
        tianqi_list = soup.find_all("span", class_="phrase")
        day_tianqi = tianqi_list[0].getText()
        night_tianqi = tianqi_list[1].getText()  # 天氣狀況
        tianqi = ''
        if day_tianqi == night_tianqi:
            tianqi = day_tianqi
        else:
            tianqi = day_tianqi + "~" + night_tianqi

        temp_list = soup.find_all("span", class_="temperature")
        day_temp = temp_list[0].getText()
        night_temp = temp_list[1].getText()
        max_temp = re.findall("\d+", day_temp)[0]  # 最高溫
        min_temp = re.findall("\d+", night_temp)[0]  # 最低溫
        # print(weatherDate +" 天氣:" + tianqi +"," + " 最高溫:" + max_temp + " , " + "最低溫:"+min_temp)
        # -----------------------------------------天氣預報基本資訊結束-----------------------------------------

        # -----------------------------------------天氣預報額外資訊開始-----------------------------------------
        other_list = soup.find_all("ul", class_="parameter")[0].contents
        for other in other_list:
            print(other.b.getText() + " : " + other.i.getText())
        # -----------------------------------------天氣預報額外資訊結束-----------------------------------------





# -----------------------------------------每天定時更新天氣預報資訊-----------------------------------------
scherduler = BackgroundScheduler()
scherduler.add_job(weatherPrediction,'interval',minutes=2)
scherduler.start()

# ----------------------------------------爬蟲獲取失敗的提醒機制,簡訊/郵件,以及切換api獲取天氣預報資訊-----------------------------------------