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获取天气预报信息-----------------------------------------

 

你可能感兴趣的:(Python)