資料結構化與儲存

2022-06-02 06:12:17 字數 1196 閱讀 9837

任務如下:

1. 將新聞的正文內容儲存到文字檔案。

2. 將新聞資料結構化為字典的列表:

3. 安裝pandas,用pandas.dataframe(newstotal),建立乙個dataframe物件df.

4. 通過df將提取的資料儲存到csv或excel 檔案。

5. 用pandas提供的函式和方法進行資料分析:

1. 將新聞的正文內容儲存到文字檔案。

#

定義乙個content.txt的文字

f = open(「content.txt」, '

a', encoding='

utf-8')

#content為文字正文

f.write(content)

f.close()

2. 將新聞資料結構化為字典的列表:

#

全域性變數

news ={}

#

第一次執行,為了獲取頁數資訊

getlistpage(listpageurl,0)

#整合一頁所有新聞

all_news.extend(page_news)

#print(page) page是我第一次獲取資訊時得到的頁數資訊

for n in range(1,page):

pageurl = listpageurl + str(n) + "

.html

"getlistpage(pageurl,n)

#整合所有頁面的所有資訊

all_news.extend(page_news)

3. 安裝pandas,用pandas.dataframe(newstotal),建立乙個dataframe物件df.

#

集合所有新聞資訊之後

df = pandas.dataframe(all_news)

4. 通過df將提取的資料儲存到csv或excel 檔案。

df.to_excel('

news.xlsx

')

5. 用pandas提供的函式和方法進行資料分析:

資料結構化與儲存

1.將新聞的正文內容儲存到文字檔案。soup beautifulsoup res.text,html.parser content soup.select show content 0 text f open news.txt w encoding utf 8 f.write content f.c...

資料結構化與儲存

作業是 同學的,因為沒有對新聞資訊做提取,所有無法新增新聞資訊到字典。已練習pandas庫的相關使用方法,匯出excel檔案。ps 自己的 會盡快修改!import requests from bs4 import beautifulsoup from datetime import datetim...

資料結構化與儲存

1.將新聞的正文內容儲存到文字檔案。newscontent soup.select show content 0 text f open news.txt w f.write newscontent f open news.txt r print f.read 3.安裝pandas,用pandas....