简单爬虫

##简单爬虫
import requests
##http://www.nmc.cn/ 天气预报-天气公告
html_dir =r"http://www.nmc.cn/publish/weather-bulletin/index.htm"
##获取网页数据
html_res = requests.get(html_dir)
##产看爬虫运行状态,200 代表正常运行,404页面无法访问
html_res.status_code
if html_res.status_code == 200:
    print(html_res.content)
from bs4 import BeautifulSoup
    dir(BeautifulSoup)
    html_info = BeautifulSoup(html_res.content,"html.parser")
html_info
type(html_info)

html_info.find_all("p")
html_info.find_all("b")
## 具体想要爬取的对象需要自己根据网页上的响应信息就行选择,一旦选择目标,只需观察该目标咋html中的存储格式标签,对这些特征进行爬取即可
for info in html_info.find_all("b"):
    print(info)
    print(str(info)[3:-4])
    ##第四个字符开始一直到倒数第四个,先转化为string再读取
with open("C:/Users/Jay/Desktop/test.txt","w") as out_file:
    for info in html_info.find_all("b"):
        print(info)
        out_file.write(str(info)[3:-4]+"\n")
## PCA
##加载数据
import plotly.express as px
df = px.data.iris()
df
df.dtypes
## 选择四列数据进行后续的分析
df_select = df.loc[:, ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']]
df_select.head()
# 探索向量之间是否有相关关系;
# 有相关关系,才需要继续做PCA
import seaborn as sns
sns.pairplot(data = df_select)

##设置四个主成分的分析功能函数
PCA_function = PCA(n_components = 4)
PCA_res = PCA_function.fit_transform(df_select)
type(PCA_res)

PCA_res.shape
PCA_res

x = np.arange(1, 5)
y = PCA_function.explained_variance_ratio_

plt.plot(x, y, '-o')
plt.xlabel("PC index")
plt.ylabel("PC explained variance ratio")
plt.grid()
plt.show()


## 根据作图结果,其实一维数据就已经能解释绝大多数数据了,但是为了后续作图的好看,我先先选两维数据
## 选取PCA结果前两个主成分
df_pca_res = pd.DataFrame(data = PCA_res[:,:2], columns=['PC1','PC2'])
df_pca_res_add_name = pd.concat([df_pca_res, df[['species']]], axis = 1)
df_pca_res_add_name.head()




## 绘图
sns.set_style('whitegrid')
sns_plot_res = sns.scatterplot(data = df_pca_res_add_name, x = 'PC1', y = 'PC2', hue = 'species')
## 图片pdf保存
sns_plot_res.figure.savefig("C:/Users/Jay/Desktop/test.pdf")

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值