Cy23
2023-01-31
来自辽宁
import requests from bs4 import BeautifulSoup import pandas as pd import matplotlib.pyplot as plt url1 = 'https://www.tianqi.com/beijing/7/' url2 = 'https://www.tianqi.com/shanghai/7/' def getHTMLtext(url): """请求获得网页内容""" headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"} try: r = requests.get(url, timeout = 30, headers = headers) r.raise_for_status() r.encoding = r.apparent_encoding print("成功访问") return r.text except: print("访问错误") return" " def get_content(city, html): """处理得到有用信息保存数据文件""" final = [] bs = BeautifulSoup(html, "html.parser") body = bs.body ul = body.find('ul', {'class': 'weaul'}) li = ul.find_all('li') for day in li: temp = [city] date = day.find(attrs={'class':'fl'}).string temp.append(date) inf = day.find_all(attrs={'class':'weaul_z'}) tem = inf[1].find_all('span') tem_low = int(tem[0].string) temp.append(tem_low) final.append(temp) return final if __name__ == '__main__': # 北京 html_text1 = getHTMLtext(url1) data1 = get_content('北京', html_text1) # 上海 html_text2 = getHTMLtext(url2) data2 = get_content('上海',html_text2) df_list = data1+data2 df = pd.DataFrame(df_list) header = ['地区', '日期', '温度'] df.columns = header x = df[df["地区"] =='北京']["日期"] y1 = df[df["地区"] =='北京']["温度"] y2 = df[df["地区"] =='上海']["温度"] # 中文乱码 plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False # 绘制折线图 plt.plot(x, y1, label="北京") plt.plot(x, y2, label="上海") plt.legend(loc="upper left") plt.title("北京与上海近7天温度情况") plt.ylabel("温度") plt.xlabel("日期") plt.show()
展开
1