我对西安近一个月天气的数据进行了爬取
import requests
from lxml import etree
import csv
import matplotlib.pyplot as plt
url = "http://www.tianqihoubao.com/weather/top/xian.html"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"
}
resp = requests.get(url,headers=headers)
html = etree.HTML(resp.text, etree.HTMLParser())
time = html.xpath("")
temple = html.xpath("")(此处路径因为上传文章存在问题,进行了删除)
#print(resp.text)
resp.close()
以下是关于可视化的相关代码
刚开始的时候没有注意,发现数据纵坐标有错乱,直接进行了颠倒,后续发现还是有错误。查了很多资料,是由于类型错误,但是我的纵坐标带有单位,无法进行类型转换。所以不知道如何解决,请大家帮忙解惑,非常感谢!
plt.rcParams['font.sans-serif'] = ['SimHei'] #这个是中文转换
plt.figure(
x=time
y=temple
plt.gca().invert_xaxis()
plt.gca().invert_yaxis()#这块就是进行颠倒
for a,b in zip(x,y):
plt.text(a,b,b,ha='center',va='bottom',fontsize=10) #这块是进行坐标展示在图上
plt.xticks(rotation=60,fontsize=5)
plt.plot(time, temple)
plt.show()
print('over!')
这个是最后的运行结果图