新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python數(shù)據(jù)模塊類如何定義
說明

1、定義數(shù)據(jù)模塊類,名為DataLoader。
2、類中有核心變量data用于保存爬行數(shù)據(jù),以及兩個(gè)相關(guān)界面grab_data(爬取數(shù)據(jù))和save_data(保存數(shù)據(jù)到當(dāng)?shù)?。
實(shí)例
grab_data() 的核心代碼
def grab_data(self): # 獲取入口鏈接 entries = self.get_entry() # 遍歷入口鏈接,解析得到文章鏈接 links = self.parse4links(entries) # 遍歷文章鏈接,解析得到文章內(nèi)容 datas = self.parse4datas(links) # 將相關(guān)數(shù)據(jù)寫入變量 data self.data = pd.DataFrame(datas)
save_data() 的核心代碼
def save_data(self): # 將變量 data 寫入 csv 文件 self.data.to_csv(self.data_path, index = None)
我們已經(jīng)爬取并保存好數(shù)據(jù) data,數(shù)據(jù)以 DataFrame 形式存儲(chǔ),保存在 csv 文件,格式如下:
|---------------------------------------------------| | id | link | cont | title | |---------------------------------------------------| | page id | page link | page content | page title | |---------------------------------------------------| | ...... | ...... | ...... | ...... | |---------------------------------------------------|
以上就是python數(shù)據(jù)模塊類定義的方法,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
名稱欄目:創(chuàng)新互聯(lián)Python教程:python數(shù)據(jù)模塊類如何定義
當(dāng)前地址:http://www.dlmjj.cn/article/dhdcccp.html


咨詢
建站咨詢
