Python学习记录——爬取天气数据
将学习的Python爬虫知识简单做个案例实战,并使用matplotlib将温度做了个图:import requestsfrom bs4 import BeautifulSoupimport matplotlib.pyplot as pltAll_Data = []def send_parse_urls():url = "http://www.weather.com.cn/weather15d/10
·
将学习的Python爬虫知识简单做个案例实战,并使用matplotlib将温度做了个图:
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import datetime
All_Data = []
def send_parse_urls():
url = "http://www.weather.com.cn/weather15d/101090101.shtml"
header = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67"}
data = requests.get(url=url, headers=header)
data.encoding = "utf-8"
# print(data.text)
soup = BeautifulSoup(data.text, "html.parser")
weekData = soup.find(name="ul", attrs={"class": "t clearfix"})
lis = weekData.find_all(name="li")
for oneDay in lis:
date = oneDay.find_all(name="span", attrs={"class": "time"})[0].text
wea = oneDay.find_all(name="span", attrs={"class": "wea"})[0].text
wind = oneDay.find_all(name="span", attrs={"class": "wind"})[0].text
wind1 = oneDay.find_all(name="span", attrs={"class": "wind1"})[0].text
tem = oneDay.find_all(name="span", attrs={"class": "tem"})[0].text
# print(date[0].text)
# print(wea[0].text)
All_Data.append({"date": date, "wea": wea, "tem": tem, "wind": wind, "wind1": wind1})
return All_Data
def main():
maxD = []
minD = []
today = datetime.datetime.now().day
days = [today,today+1,today+2,today+3,today+4,today+5,today+6,today+7]
wea_data = send_parse_urls()
for oneDayWea in wea_data:
tem = oneDayWea["tem"]
maxTem = tem.split("/")[0].replace("℃","")
minTem = tem.split("/")[1].replace("℃","")
minD.append(int(minTem))
maxD.append(int(maxTem))
plt.title("SJZ Weather")
plt.xlabel("date")
plt.ylabel("tem")
# ax = plt.gca()
# ax.invert_yaxis()
plt.plot(days,maxD,"r")
plt.plot(days,minD,"b")
#plt.savefig('testblueline.jpg')
plt.show()
if __name__ == '__main__':
main()

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)