Pandas中的read_html()
其允许从网页中提取表格数据,并将其直接转换为pandas的DataFrame对象,也就是说它只针对网页上有标签的表格数据进行爬取,如果没有这个标签的话,会提示“No tables found”
以天气后报网站为例,https://www.tianqihoubao.***/weather/top/shanghai.html
我们想要爬取的是上海市最近的天气情况,查看网页源代码
只要三行代码就可以爬取下来:
import pandas as pd
df=pd.read_html("http://www.tianqihoubao.***/weather/top/shanghai.html", encoding='utf-8', header=0)[0]
df=df.iloc[1:,:]
#print(df)
爬取效果如下,如果希望保存到文件中,可以用pd.to_excel():
功能yyds!