最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python數(shù)據(jù)分析之將爬取的數(shù)據(jù)保存為csv格式

 更新時間:2022年06月13日 10:11:33   作者:軒軒是只橘豬豬  
Python內置了CSV模塊,可直接通過該模塊實現(xiàn)csv文件的讀寫操作,在web應用中導出數(shù)據(jù)是比較常見操作,下面這篇文章主要給大家介紹了關于python數(shù)據(jù)分析之將爬取的數(shù)據(jù)保存為csv格式的相關資料,需要的朋友可以參考下

csv文件

一種用逗號分割來實現(xiàn)存儲表格數(shù)據(jù)的文本文件。

python的csv模塊

python遍歷代碼:

arr = [12, 5, 33, 4, 1]
#遍歷輸出1
for i in range(0, len(arr)):
    item = arr[i]
    print(item)

#遍歷輸出2
for item in arr:
    print(item)

#遍歷輸出3
string_arr = ["hi", "hello", "你好", "aloha"]
for item in string_arr:
    print("本次循環(huán) item 變量的值", item)

從csv文件讀取內容

用DictReader對象的創(chuàng)建方法以及通過filenames屬性獲取csv表格的表頭。

import csv
#打開csv
fo = open("info.csv")
#打開csv文件的文件對象作為參數(shù)來創(chuàng)建dictreader類的對象,存在reader變量中
reader = csv.DictReader(fo)
#調用reader對象的filednames屬性,獲取csv文件表格的開頭
headers = reader.fieldnames
#關閉文件
fo.close()
#打印
print(headers)

獲取表格實際內容。

fo = open("info.csv")
reader = csv.DictReader(fo)
#創(chuàng)建列表,存儲讀到的行
row_list = []
#遍歷
for row in reader:
    row_list.append(row)
fo.close()
#打印
print(row_list[0])
#遍歷row_list
for d in row_list:
    #d是字典,直接打印key為年齡值即可
    print(d["年齡"])
#打印

寫入csv文件

python提供了DictWriter方法,可以講表格數(shù)據(jù)以字典的形式存儲到csv文件中。

import csv
#打開一個文件,假設是info.csv,寫入所以是w
#newline='',寫入時需要指定
fo = open("info2.csv", "w", newline='')
#將表頭存儲到一個列表里
header = ["姓名", "年齡", "部門"]
#創(chuàng)建一個DictWriter對象,第二個參數(shù)就是上面創(chuàng)建的表頭
writer = csv.DictWriter(fo, header)
writer.writeheader()
#寫入一行記錄,以字典的形式,key需要與表頭對應
writer.writerow({"姓名": "小明", "年齡":"28", "部門": "行政部"})
#關閉文件
fo.close()

運行后,相應的文件夾下會出現(xiàn)一個對應的csv文件。

也可以使用writer.writerows(row_list)來寫入多個。

運用實例

數(shù)據(jù)準備

1、打開網(wǎng)頁,讀取內容,并創(chuàng)建相應的BeautifulSoup對象

2、找到包含新聞的div元素列表

3、從2中抽取標題

4、從2中抽取時間

from bs4 import BeautifulSoup
def create_doc_from_filename(filename):
    fo = open(filename, "r", encoding='utf-8')
    html_content = fo.read()
    fo.close
    doc = BeautifulSoup(html_content)
    return doc

(記得要pip install bs4)

#輸入?yún)?shù)是BeautifulSoup對象,返回包含新聞的div元素列表
def find_index_labels(doc):
    index_labels = doc.find_all("div", class_ = "indexs")
    return index_labels
#實現(xiàn)新聞標題的抽取函數(shù)
def get_title(label_object):
    #從剛才的參數(shù)傳入的標簽對象中過濾出所有的target = _blank的a標簽
    a_labels = label_object.find_all("a", target = "_blank")
    #取得第一個標簽對象
    my_label = a_labels[0]
    #將標簽的文字內容作為返回值返回
    return my_label.get_text()
#實現(xiàn)獲取新聞發(fā)布時間的函數(shù)
def get_pub_time(label_object):
    #找到class = comment-link的span標簽
    spans = label_object.find_all("span", class_ = "comment-link")
    #取第一個
    span = spans[0]
    #返回標題屬性
    return span["title"]
#獲取新聞標題與列表
#調用create_doc_from_filename函數(shù)
doc = create_doc_from_filename("jiandan.html")
#傳入BeautifulSoup對象,將返回的div列表存儲在index_labels中
index_labels = find_index_labels(doc)
for label_object in index_labels:
    title = get_title(label_object)
    pub_time = get_pub_time(label_object)
    print("標題", title)
    print("發(fā)布時間", pub_time)

將數(shù)據(jù)存為字典的形式

#獲取新聞標題與列表
#調用create_doc_from_filename函數(shù)
doc = create_doc_from_filename("jiandan.html")
#傳入BeautifulSoup對象,將返回的div列表存儲在index_labels中
index_labels = find_index_labels(doc)
news_dict_list = []
for label_object in index_labels:
    title = get_title(label_object)
    pub_time = get_pub_time(label_object)
    news = {"標題": title, "發(fā)布時間": pub_time}
    news_dict_list.append(news)
print(news_dict_list)

存儲到csv文件

#創(chuàng)建csv
fo = open("news.csv", "w", newline='', encoding='utf-8')
#表頭
header = ["標題", "發(fā)布時間"]
writer = csv.DictWriter(fo, header)
#寫入表頭
writer.writeheader()
#將上一步的字典寫入csv文件中
writer.writerows(news_dict_list)
fo.close()

總結

到此這篇關于python數(shù)據(jù)分析之將爬取的數(shù)據(jù)保存為csv格式的文章就介紹到這了,更多相關python爬取數(shù)據(jù)保存csv格式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

延吉市| 清河县| 大关县| 北京市| 高台县| 江北区| 灵川县| 洪湖市| 孝昌县| 奉节县| 汉中市| 慈利县| 高碑店市| 奉化市| 双柏县| 资溪县| 永州市| 梅河口市| 天全县| 泰和县| 双牌县| 枣庄市| 上高县| 新乡市| 东丰县| 泸水县| 乌兰察布市| 龙南县| 个旧市| 仙桃市| 庄浪县| 新和县| 瑞丽市| 长岭县| 陈巴尔虎旗| 栾城县| 大名县| 北宁市| 宕昌县| 山阳县| 韩城市|