鱼C论坛

 找回密码
 立即注册
查看: 2161|回复: 0

[作品展示] 爬取每月天气变化并生成折线图

[复制链接]
发表于 2021-7-9 22:18:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 风船野 于 2021-7-10 22:09 编辑

输入年月和城市,爬取该城市当月天气,并生成折线图
代码包括爬虫,可视化,和图形界面三大部分。
简易图形界面:
01.png
结果展示:
西安3月份每日温度变化表.png

代码:
  1. import requests
  2. from matplotlib import pyplot as plt, font_manager
  3. import bs4
  4. from xpinyin import Pinyin
  5. from tkinter import *


  6. # 创建网址链接
  7. def urls():
  8.     global dates
  9.     p = Pinyin()
  10.     city = e1.get()
  11.     city = p.get_pinyin(city)  # 汉字转为拼音
  12.     city = city.replace("-", "")
  13.     dates = variable1.get() + variable2.get()
  14.     url = f"https://lishi.tianqi.com/{city}/{dates}.html".format(city=city, dates=dates)
  15.     print(url)
  16.     return url


  17. # 爬取网页数据
  18. def open_url(url):
  19.     headers = {
  20.         "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,"
  21.                   "application/signed-exchange;v=b3;q=0.9",
  22.         "sec - fetch - site": "same - origin",
  23.         "accept - encoding": "gzip, deflate, br",
  24.         "cache-control": "max-age=0",
  25.         "accept-language": "zh-CN,zh;q=0.9",
  26.         "referer": "https://www.tianqi.com/qiwen/city-xian-3/",
  27.         "upgrade - insecure - requests": "1",
  28.         "cokkie": "UM_distinctid=17a7045d2b737c-09469a16397897-6373264-149c48-17a7045d2b8b9a; "
  29.                   "Hm_lvt_ab6a683aa97a52202eab5b3a9042a8d2=1625381262; "
  30.                   "CNZZDATA1275796416=1699660372-1625376765-https%253A%252F%252Fwww.google.com.hk%252F%7C1625398369; "
  31.                   "Hm_lpvt_ab6a683aa97a52202eab5b3a9042a8d2=1625402066",
  32.         "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
  33.                       "Chrome/91.0.4472.124 Safari/537.36"}
  34.     res = requests.get(url, headers=headers)
  35.     return res


  36. # 数据搜索
  37. def find_s(res):
  38.     temperature = []

  39.     soup = bs4.BeautifulSoup(res.text, "html.parser")
  40.     targets = soup.find("ul", class_="thrui")
  41.     targets = targets.find_all("div", class_="th140")
  42.     for each in targets:
  43.         temperature.append(each.text)
  44.     return temperature


  45. # 数据分组处理
  46. def handles_list(temperature):
  47.     global temperature_max
  48.     temperature_max, temperature_min = [], []
  49.     for i in range(0, len(temperature), 4):
  50.         temperature_max.append(temperature[i].replace("℃", ""))
  51.         temperature_min.append(temperature[i + 1].replace("℃", ""))
  52.     return temperature_max, temperature_min


  53. # 制作折线图
  54. def drawing(y_max, y_min):
  55.     city = e1.get()
  56.     data = dates[5]

  57.     y_max = list(map(float, y_max))
  58.     y_min = list(map(float, y_min))
  59.     y_max = list(map(int, y_max))
  60.     y_min = list(map(int, y_min))
  61.     my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\STKAITI.TTF")
  62.     plt.figure(figsize=(20, 9), dpi=100)

  63.     x = range(1, len(temperature_max) + 1)

  64.     plt.plot(x, y_max, label="最高温度", color="red")
  65.     plt.plot(x, y_min, label="最低温度", color="cyan")

  66.     xl = [data + "月{}日".format(i) for i in x]
  67.     plt.xticks(x, xl, fontproperties=my_font, rotation=40)
  68.     plt.yticks(range(min(y_min + y_max), max(y_min + y_max) + 4, 2))
  69.     plt.xlabel("时间", fontproperties=my_font)
  70.     plt.ylabel("温度", fontproperties=my_font)
  71.     plt.title("{city}{data}月份每日温度变化表".format(city=city, data=data), fontproperties=my_font)

  72.     plt.legend(prop=my_font)
  73.     plt.grid(alpha=0.8)
  74.     plt.savefig("./{city}{data}月份每日温度变化表.png".format(city=city, data=data))
  75.     plt.show()


  76. def main():
  77.     try:
  78.         url = urls()
  79.         res = open_url(url)
  80.         temperature = find_s(res)
  81.         y_max, y_min = handles_list(temperature)
  82.         drawing(y_max, y_min)
  83.     except AttributeError:
  84.         lab5.grid(row=2, column=1, columnspan=4, rowspan=2, padx=10, pady=10)


  85. # 图形界面
  86. root = Tk()
  87. f1 = Frame(root).grid()
  88. lab1 = Label(f1, text="日期:").grid(row=0, column=0)
  89. lab2 = Label(f1, text="城市:").grid(row=1, column=0)

  90. variable1 = StringVar()
  91. variable2 = StringVar()
  92. variable1.set("2021")
  93. variable2.set("01")
  94. w = OptionMenu(f1, variable1, "2021", "2020", "2019", "2018", "2017", "2016", "2015", "2014", "2013"). \
  95.     grid(row=0, column=1)
  96. lab3 = Label(f1, text="年").grid(row=0, column=2)
  97. w2 = OptionMenu(f1, variable2, "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12") \
  98.     .grid(row=0, column=3)
  99. lab4 = Label(f1, text="月").grid(row=0, column=4)
  100. e1 = Entry(f1)
  101. e1.grid(row=1, column=1, columnspan=4)

  102. b1 = Button(f1, text="确定", width=10, command=main).grid(row=1, column=5, sticky=W, padx=10, pady=5)
  103. lab5 = Label(f1, text="数据还没统计,等等再来吧!!!", foreground="red", font=("宋体", 10))

  104. mainloop()
复制代码

希望各位大佬指正存在问题。感激不尽。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 20:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表