Python天气查询系统(python爬虫期末小作品作业)

【附代码】python爬虫期末小作品作业,使用selenium,matplotlib,用chromedriver获取数据,并使用pyttsx3库,给三个功能都设置语音播报,使用tkinter给第一个功能增加弹窗

有三个功能:  某地某年某月某日爬取

                        某地某年某个月的全部天数的爬取

                        某地某年12个月的天气,

                        某地某年到某年的所有月份平均温度

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import matplotlib.pyplot as plt
import pyttsx3
import tkinter as tk

#语音播报准备
engine = pyttsx3.init()# 创建TTS引擎对象
engine.setProperty('voice.pcm', 'path/to/voice.wav')# 设置语音文件路径
engine.setProperty('rate', 150)# 设置语速
engine.setProperty('voice.male', True)# 设置播报模式

# 创建主窗口
root = tk.Tk()
root.geometry('900x600')
root.title('查询天气')


driver = webdriver.Chrome()


def mainpage():   #功能主页
    print("*******天气查询系统*******\n"
          "1  某地某年某月某日爬取\n"
          "2  某年的某个月一整月的气温爬取\n"
          "3  某地某年12个月的天气\n"
          "4  某地某年到某年的所有月份平均温度\n"
          "5  退出\n"
          "************************")


def driverOneDay():     #爬取一天的温度数据
    city = input("请输入你想查询的城市:")
    year = input("请输入你想查询年份:")
    month = input("请输入你想查询的月份:")
    day = input("请输入你想查询的日期:")
    driver.get("https://lishi.tianqi.com/%s/%s%s.html" % (city, year, month))

    place = driver.find_element(By.XPATH, "/html/body/div[7]/div[1]/div[3]/div/div[1]/h3").text  # 爬取的城市
    date = driver.find_element(By.XPATH, "/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[1]" % day).text  # 爬取的年、月、日
    maxTemperature = driver.find_element(By.XPATH, "/html/body/div[7]/div[1]/div[3]/ul/li[1]/div[1]/div[1]").text  # 最高温度
    minTemperature = driver.find_element(By.XPATH,"/html/body/div[7]/div[1]/div[3]/ul/li[1]/div[2]/div[1]").text  # 最低温度
    averageTemperature = (int(maxTemperature[:-1]) + int(minTemperature[:-1])) / 2  # 平均温度
    AverageAirQualityIndex = driver.find_element(By.XPATH,"/html/body/div[7]/div[1]/div[3]/ul/li[4]/div[1]").text  # 平均空气质量指数

    print("地点:", place)
    print("日期:", date)
    print("最高温度:", maxTemperature)
    print("最低温度:", minTemperature)
    print("平均温度:", averageTemperature, "℃")
    print("平均空气质量指数:", AverageAirQualityIndex)

    # 文本
    content = '您所查询的%s\n%s,\n最高气温%s,\n最低气温%s,\n平均气温%s摄氏度,\n平均空气质量指数%s\n' % (place, date, maxTemperature, minTemperature, averageTemperature, AverageAirQualityIndex)
    #tkinter界面
    text = content
    label = tk.Label(root, text=text, font=("楷体", 24), fg="blue")
    label.pack(pady=50)
    # 显示界面
    root.mainloop()
    #语音播报
    engine.say(content)
    engine.runAndWait()  # 等待用户关闭TTS引擎


def driverOneMonth():  #爬取的一个月的温度
    city = input("请输入你想查询的城市:")
    year = input("请输入你想查询年份:")
    month = input("请输入你想要查询的月份:")

    driver.get("https://lishi.tianqi.com/%s/%s%s.html" % (city, year, month))
    place = driver.find_element(By.XPATH, "/html/body/div[7]/div[1]/div[3]/div/div[1]/h3").text  # 爬取的城市
    print("地点:", place)
    f = driver.find_element(By.XPATH, "/html/body/div[7]/div[1]/div[4]/ul/div")
    f.click()

    Temperature = []
    day = []

    if month in ["01", "03", "05", "07", "08", "10", "12"]:
        for i in range(1, 32):
            day.append(i)
            date = driver.find_element(By.XPATH,
                                       "/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[1]" % i).text  # 爬取的年、月、日
            maxTemperature = driver.find_element(By.XPATH,
                                                 "/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[2]" % i).text  # 最高温度
            minTemperature = driver.find_element(By.XPATH,
                                                 "/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[3]" % i).text  # 最低温度
            averageTemperature = (int(maxTemperature[:-1]) + int(minTemperature[:-1])) / 2  # 平均温度
            Temperature.append(averageTemperature)
            print("日期:", date)
            print("最高温度:", maxTemperature)
            print("最低温度:", minTemperature)
            print("平均温度:", averageTemperature, "℃")
            print(" ")
    elif month in ["04", "06", "09", "11"]:
        for i in range(1, 31):
            day.append(i)
            date = driver.find_element(By.XPATH,
                                       "/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[1]" % i).text  # 爬取的年、月、日
            maxTemperature = driver.find_element(By.XPATH,
                                                 "/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[2]" % i).text  # 最高温度
            minTemperature = driver.find_element(By.XPATH,
                                                 "/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[3]" % i).text  # 最低温度
            averageTemperature = (int(maxTemperature[:-1]) + int(minTemperature[:-1])) / 2  # 平均温度
            Temperature.append(averageTemperature)
            print("日期:", date)
            print("最高温度:", maxTemperature)
            print("最低温度:", minTemperature)
            print("平均温度:", averageTemperature, "℃")
            print(" ")
    else:
        if month in ["02"]:
            if (int(year) % 4) == 0 and (int(year) % 100) != 0 or (int(year) % 400) == 0:
                for i in range(1, 30):
                    day.append(i)
                    date = driver.find_element(By.XPATH,"/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[1]" % i).text  # 爬取的年、月、日
                    maxTemperature = driver.find_element(By.XPATH,"/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[2]" % i).text  # 最高温度
                    minTemperature = driver.find_element(By.XPATH, "/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[3]" % i).text  # 最低温度
                    averageTemperature = (int(maxTemperature[:-1]) + int(minTemperature[:-1])) / 2  # 平均温度
                    Temperature.append(averageTemperature)
                    print("日期:", date)
                    print("最高温度:", maxTemperature)
                    print("最低温度:", minTemperature)
                    print("平均温度:", averageTemperature, "℃")
                    print(" ")
            else:
                for i in range(1, 29):
                    day.append(i)
                    date = driver.find_element(By.XPATH, "/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[1]" % i).text  # 爬取的年、月、日
                    maxTemperature = driver.find_element(By.XPATH, "/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[2]" % i).text  # 最高温度
                    minTemperature = driver.find_element(By.XPATH, "/html/body/div[7]/div[1]/div[4]/ul/li[%s]/div[3]" % i).text  # 最低温度
                    averageTemperature = (int(maxTemperature[:-1]) + int(minTemperature[:-1])) / 2  # 平均温度
                    Temperature.append(averageTemperature)
                    print("日期:", date)
                    print("最高温度:", maxTemperature)
                    print("最低温度:", minTemperature)
                    print("平均温度:", averageTemperature, "℃")
                    print(" ")

    # 文本
    content = "您所查询的该地区%s年%s月,\n最高平均气温%s摄氏度,\n最低平均气温%s摄氏度\n" % (year, month, max(Temperature), min(Temperature))

    # 播放语音
    engine.say(content)
    engine.runAndWait()  # 等待用户关闭TTS引擎

    # 画折线图
    plt.plot(day, Temperature)  # 横坐标为月,纵坐标为温度
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False  # 设置正常显示符号
    plt.legend(labels=['%s年' % year])
    plt.xlabel('天数')
    plt.ylabel('温度')
    plt.title('%s年%s月的温度' % (year, month))
    plt.show()


def driverOneYear():        #爬取的一年的温度
    city = input("请输入你想查询的城市:")
    year = input("请输入你想查询年份:")
    Temperature = []  # 存储爬取到的温度的列表

    month = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
    for i in month:
        driver.get("https://lishi.tianqi.com/%s/%s%s.html" % (city, year, i))
        place = driver.find_element(By.XPATH, "/html/body/div[7]/div[1]/div[3]/div/div[1]/h3").text
        averageTemperature = driver.find_element(By.XPATH,"/html/body/div[7]/div[1]/div[3]/ul/li[1]/div[1]/div[1]").text
        Temperature.append(int(averageTemperature[:-1]))  # 将爬取到的温度存储到列表
        print("%s%s年%s月平均温度:" % (place, year, i), averageTemperature)

    # 文本
    content = "您所查询的该地区%s年,\n最高平均气温%s摄氏度,\n最低平均气温%s摄氏度\n" % (year, max(Temperature), min(Temperature))

    # 播放语音
    engine.say(content)
    engine.runAndWait()  # 等待用户关闭TTS引擎

    # 画折线图
    plt.plot(month, Temperature)  # 横坐标为月,纵坐标为温度
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False  # 设置正常显示符号
    plt.legend(labels=['%s年' % year])
    plt.xlabel('月')
    plt.ylabel('温度')
    plt.title('%s年12个月的温度' % year)
    plt.show()


def driverManyYear():    #爬取三年的温度
    city = input("请输入你想看的城市:")
    year1, year2, year3 = map(int, input("请输入你想查看的三个年份:").split())  #一行输入,空格隔开
    year = [year1, year2, year3]
    month = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
    Temperature1 = []   #存储第一年爬取到的温度的列表
    Temperature2 = []   #存储第二年爬取到的温度的列表
    Temperature3 = []   #存储第三年爬取到的温度的列表

    for y in range(len(year)):
        for i in month:
            driver.get("https://lishi.tianqi.com/%s/%s%s.html" % (city, year[y], i))
            place = driver.find_element(By.XPATH, "/html/body/div[7]/div[1]/div[3]/div/div[1]/h3").text
            averageTemperature = driver.find_element(By.XPATH,"/html/body/div[7]/div[1]/div[3]/ul/li[1]/div[1]/div[1]").text
            if y == 0:  #将爬取到的第一年的温度存储到列表
                Temperature1.append(int(averageTemperature[:-1]))
            elif y == 1:    #将爬取到的第二年的温度存储到列表
                Temperature2.append(int(averageTemperature[:-1]))
            else:   #将爬取到的第三年的温度存储到列表
                Temperature3.append(int(averageTemperature[:-1]))

            print("%s%s年%s月平均温度:" % (place, year[y], i), averageTemperature)

    # 文本
    content = "您所查询的该地区这三年中,%s年最高平均气温为%s摄氏度,最低平均气温%s摄氏度,\n" \
              "%s年最高平均气温%s摄氏度,最低平均气温%s摄氏度,\n" \
              "%s年最高平均气温%s摄氏度,最低平均气温%s摄氏度\n" % (year1, max(Temperature1), min(Temperature1),
                                                                 year2, max(Temperature2), min(Temperature2),
                                                                 year3, max(Temperature1), min(Temperature1))

    # 播放语音
    engine.say(content)
    engine.runAndWait()  # 等待用户关闭TTS引擎

    # 画折线图
    plt.plot(month, Temperature1, color='red', linewidth=3.0)
    plt.plot(month, Temperature2, color='blue', linewidth=3.0)
    plt.plot(month, Temperature3, color='black', linewidth=3.0)
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False  # 设置正常显示符号
    plt.legend(['%s' % year1, '%s' % year2, '%s' % year3])
    plt.xlabel('月')
    plt.ylabel('温度')
    plt.show()


while True:
    mainpage()
    option = int(input("请输入所需要功能对应的数字:"))
    if option == 1:
        driverOneDay()
    elif option == 2:
        driverOneMonth()
    elif option == 3:
        driverOneYear()
    elif option == 4:
        driverManyYear()
    elif option == 5:
        print("已退出系统")
        break
    else:
        print("数字输入有误,无对应功能,请重新输入正确的数字")

你可能感兴趣的:(python,爬虫,课程设计)