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

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

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

csv文件

一種用逗號(hào)分割來實(shí)現(xiàn)存儲(chǔ)表格數(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文件讀取內(nèi)容

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

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

獲取表格實(shí)際內(nèi)容。

fo = open("info.csv")
reader = csv.DictReader(fo)
#創(chuàng)建列表,存儲(chǔ)讀到的行
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ù)以字典的形式存儲(chǔ)到csv文件中。

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

運(yùn)行后,相應(yīng)的文件夾下會(huì)出現(xiàn)一個(gè)對(duì)應(yīng)的csv文件。

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

運(yùn)用實(shí)例

數(shù)據(jù)準(zhǔn)備

1、打開網(wǎng)頁,讀取內(nèi)容,并創(chuàng)建相應(yīng)的BeautifulSoup對(duì)象

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

3、從2中抽取標(biāo)題

4、從2中抽取時(shí)間

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

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

#獲取新聞標(biāo)題與列表
#調(diào)用create_doc_from_filename函數(shù)
doc = create_doc_from_filename("jiandan.html")
#傳入BeautifulSoup對(duì)象,將返回的div列表存儲(chǔ)在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 = {"標(biāo)題": title, "發(fā)布時(shí)間": pub_time}
    news_dict_list.append(news)
print(news_dict_list)

存儲(chǔ)到csv文件

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

總結(jié)

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

相關(guān)文章

最新評(píng)論

岳阳市| 阿合奇县| 都安| 荣昌县| 池州市| 宁海县| 若尔盖县| 靖西县| 同心县| 宜都市| 兴海县| 千阳县| 朝阳县| 哈密市| 海林市| 紫金县| 怀柔区| 石首市| 独山县| 博野县| 锡林浩特市| 灵台县| 阿城市| 上高县| 岳池县| 洪雅县| 康定县| 德昌县| 图们市| 普宁市| 宣恩县| 鄯善县| 丹巴县| 婺源县| 青川县| 河源市| 松江区| 丘北县| 龙山县| 海林市| 博湖县|