資料結構化與儲存

2022-07-15 12:48:10 字數 3784 閱讀 2640

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

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

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

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

import

requests, re, pandas, openpyxl

from bs4 import

beautifulsoup

from datetime import

datetime

#獲取新聞點選次數

defgetnewsclick(url):

newsid = re.search(r'

\_\d\/(.*).html

', url).group(1)

clickurl = '

'.format(newsid)

clickres =requests.get(clickurl)

#利用正規表示式獲取新聞點選次數

clickcount = int(re.search("

hits'\).html\('(.*)'\);

", clickres.text).group(1))

return

clickcount

#獲取新聞細節

defgetnewsdetail(newsurl):

resd =requests.get(newsurl)

resd.encoding = '

utf-8

'soupd = beautifulsoup(resd.text, '

html.parser')

newsdict ={}

content = soupd.select('

#content

')[0].text

info = soupd.select('

.show-info

')[0].text

newsdict[

'title

'] = soupd.select('

.show-title

')[0].text

date = re.search('

(\d.\d.\d\s\d.\d.\d)

', info).group(1) #

識別時間格式

#識別乙個至三個資料

if info.find('

作者') >0:

newsdict[

'author

'] = re.search('

', info).group(1)

else

: newsdict[

'author

'] = '

none

'if info.find('

審核') >0:

newsdict[

'check

'] = re.search('

', info).group(1)

else

: newsdict[

'check

'] = '

none

'if info.find('

**') >0:

first = re.search('

', info).group(1)

if first.find('

攝影') >0:

newsdict[

'sources

'] = re.search('

(.*)\s*攝

', first).group(1) #

解決新聞有攝影無法匹配正則的問題

else

: newsdict[

'sources

'] =first

else

: newsdict[

'sources

'] = '

none

'if info.find('

攝影') >0:

newsdict[

'photo

'] = re.search('

攝影:(.*)\s*點

', info).group(1)

else

: newsdict[

'photo

'] = '

none

'newsdict[

'datetime

'] = datetime.strptime(date, '

%y-%m-%d %h:%m:%s

') #

用datetime將時間字串轉換為datetime型別

newsdict['

click

'] = getnewsclick(newsurl) #

呼叫getnewsclick()獲取點選次數

newsdict['

content

'] =content

return

newsdict

def getlistpage(listurl): #

獲取一頁的新聞,轉換成列表返回

res =requests.get(listurl)

res.encoding = '

utf-8

'listsoup = beautifulsoup(res.text, '

html.parser')

pagelist =

for new in listsoup.select('

.news-list

')[0].select('li'

): newsurl = new.select('

a')[0]['

href']

pagedict = getnewsdetail(newsurl) #

呼叫getnewsdetail()獲取新聞詳情

#break

return

pagelist

def gettotalpage(listurl): #

獲取總新聞頁面

res =requests.get(listurl)

res.encoding = '

utf-8

'soup = beautifulsoup(res.text, '

html.parser')

return int(soup.select('

.a1')[0].text.rstrip('

條')) // 10 + 1total =

listurl = '

'pagelist =getlistpage(listurl)

total.extend(pagelist)

listcount =gettotalpage(listurl)

pan =pandas.dataframe(total)

pan.to_excel(

'result.xlsx

') #

匯出為excel**

pan.to_csv('

result.csv

') #

匯出為csv檔案

for i in range(2,listcount):

listurl= '

{}.html

'.format(i)

total = getlistpage(listurl)

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....