溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python如何爬取哈利波特小說

發(fā)布時間:2021-11-25 15:05:56 來源:億速云 閱讀:166 作者:iii 欄目:大數(shù)據(jù)

本篇內容主要講解“Python如何爬取哈利波特小說”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Python如何爬取哈利波特小說”吧!

先簡單介紹一下jieba中文分詞包,jieba包主要有三種分詞模式:

  • 精確模式:默認情況下是精確模式,精確地分詞,適合文本分析;

  • 全模式:把所有能成詞的詞語都分出來, 但是詞語會存在歧義;

  • 搜索引擎模式:在精確模式的基礎上,對長詞再次切分,適合用于搜索引擎分詞。

jieba 包常用的語句:

  • 精確模式分詞:jieba.cut(text,cut_all = False),當cut_all = True時為全模式

  • 自定義詞典:jieba.load_userdict(file_name)

  • 增加詞語:jieba.add_word(seg,freq,flag)

  • 刪除詞語:jieba.del_word(seg)

《哈利·波特》是英國作家J·K·羅琳的奇幻文學系列小說,描寫主角哈利·波特在霍格沃茨魔法學校7年學習生活中的冒險故事。下面將以《哈利波特》錯綜復雜的人物關系為例,實踐一下jieba包。

#加載所需包
import numpy as np
import pandas as pd
import jieba,codecs
import jieba.posseg as pseg  #標注詞性模塊
from pyecharts import Bar,WordCloud

#導入人名、停用詞、特定詞庫
renmings = pd.read_csv('人名.txt',engine='python',encoding='utf-8',names=['renming'])['renming']
stopwords = pd.read_csv('mystopwords.txt',engine='python',encoding='utf-8',names=['stopwords'])['stopwords'].tolist()
book = open('哈利波特.txt',encoding='utf-8').read()
jieba.load_userdict('哈利波特詞庫.txt')

#定義一個分詞函數(shù)
def words_cut(book):
    words = list(jieba.cut(book))
    stopwords1 = [w for w in words if len(w)==1]  #添加停用詞
    seg = set(words) - set(stopwords) - set(stopwords1) #過濾停用詞,得到更為精確的分詞
    result = [i for i in words if i in seg]
    return result

#初次分詞
bookwords = words_cut(book)
renming = [i.split(' ')[0] for i in set(renmings)] #只要人物名字,出掉詞頻以及詞性
nameswords = [i for i in bookwords if i in set(renming)]  #篩選出人物名字

#統(tǒng)計詞頻
bookwords_count = pd.Series(bookwords).value_counts().sort_values(ascending=False)
nameswords_count = pd.Series(nameswords).value_counts().sort_values(ascending=False)
bookwords_count[:100].index

經過初次分詞之后,我們發(fā)現(xiàn)大部分的詞語已經ok了,但是還是有小部分名字類的詞語分得不精確,比如說'布利'、'羅恩說'、'伏地'、'斯內'、'地說'等等,還有像'烏姆里奇'、'霍格沃茲'等分成兩個詞語的。

#自定義部分詞語
jieba.add_word('鄧布利多',100,'nr')
jieba.add_word('霍格沃茨',100,'n')
jieba.add_word('烏姆里奇',100,'nr')
jieba.add_word('拉唐克斯',100,'nr')
jieba.add_word('伏地魔',100,'nr')
jieba.del_word('羅恩說')
jieba.del_word('地說')
jieba.del_word('斯內')

#再次分詞
bookwords = words_cut(book)
nameswords = [i for i in bookwords if i in set(renming)]
bookwords_count = pd.Series(bookwords).value_counts().sort_values(ascending=False)
nameswords_count = pd.Series(nameswords).value_counts().sort_values(ascending=False)
bookwords_count[:100].index

Python如何爬取哈利波特小說

再次分詞之后,我們可以看到在初次分詞出現(xiàn)的錯誤已經得到修正了,接下來我們統(tǒng)計分析。

#統(tǒng)計詞頻TOP15的詞語
bar = Bar('出現(xiàn)最多的詞語TOP15',background_color = 'white',title_pos = 'center',title_text_size = 20)
x = bookwords_count[:15].index.tolist()
y = bookwords_count[:15].values.tolist()
bar.add('',x, y,xaxis_interval = 0,xaxis_rotate = 30,is_label_show = True)
bar

整部小說出現(xiàn)最多的詞語TOP15中出現(xiàn)了哈利、赫敏、羅恩、鄧布利多、魔杖、魔法、馬爾福、斯內普和小天狼星等字眼。

我們自己串一下,大概可以知道《哈利波特》的主要內容了,就是哈利在小伙伴赫敏、羅恩的陪伴下,經過大法師鄧布利多的幫助與培養(yǎng),利用魔杖使用魔法把大boss伏地魔k.o的故事。當然啦,《哈利波特》還是非常精彩的。

#統(tǒng)計人物名字TOP20的詞語
bar = Bar('主要人物Top20',background_color = 'white',title_pos = 'center',title_text_size = 20)
x = nameswords_count[:20].index.tolist()
y =nameswords_count[:20].values.tolist()
bar.add('',x, y,xaxis_interval = 0,xaxis_rotate = 30,is_label_show = True)
bar

Python如何爬取哈利波特小說

整部小說按照出場次數(shù),我們發(fā)現(xiàn)哈利作為主角的地位無可撼動,比排名第二的赫敏遠超13000多次,當然這也是非常正常的,畢竟這本書是《哈利波特》,而不是《赫敏格蘭杰》。

#整本小說的詞語詞云分析
name = bookwords_count.index.tolist()
value = bookwords_count.values.tolist()
wc = WordCloud(background_color = 'white')
wc.add("", name, value, word_size_range=[10, 200],shape = 'diamond')
wc
#人物關系分析
names = {} 
relationships = {} 
lineNames = []
with codecs.open('哈利波特.txt','r','utf8') as f:
    n = 0
    for line in f.readlines(): 
        n+=1
        print('正在處理第{}行'.format(n))
        poss = pseg.cut(line)
        lineNames.append([])
        for w in poss:
            if w.word in set(nameswords):
                lineNames[-1].append(w.word)
                if names.get(w.word) is None:
                    names[w.word] = 0
                    relationships[w.word] = {} 
                names[w.word] += 1
for line in lineNames:
    for name1 in line:
        for name2 in line:
            if name1 == name2:
                continue
            if relationships[name1].get(name2) is None:
                relationships[name1][name2]= 1
            else:
                relationships[name1][name2] = relationships[name1][name2]+ 1
node = pd.DataFrame(columns=['Id','Label','Weight'])
edge = pd.DataFrame(columns=['Source','Target','Weight'])
for name,times in names.items():
        node.loc[len(node)] = [name,name,times]
for name,edges in relationships.items():
        for v, w in edges.items():
            if w > 3:
                edge.loc[len(edge)] = [name,v,w]

處理之后,我們發(fā)現(xiàn)同一個人物出現(xiàn)了不同的稱呼,因此合并并統(tǒng)計,得出88個節(jié)點。

node.loc[node['Id']=='哈利','Id'] = '哈利波特'
node.loc[node['Id']=='波特','Id'] = '哈利波特'
node.loc[node['Id']=='阿不思','Id'] = '鄧布利多'
node.loc[node['Label']=='哈利','Label'] = '哈利波特'
node.loc[node['Label']=='波特','Label'] = '哈利波特'
node.loc[node['Label']=='阿不思','Label'] = '鄧布利多'
edge.loc[edge['Source']=='哈利','Source'] = '哈利波特'
edge.loc[edge['Source']=='波特','Source'] = '哈利波特'
edge.loc[edge['Source']=='阿不思','Source'] = '鄧布利多'
edge.loc[edge['Target']=='哈利','Target'] = '哈利波特'
edge.loc[edge['Target']=='波特','Target'] = '哈利波特'
edge.loc[edge['Target']=='阿不思','Target'] = '鄧布利多'
nresult = node['Weight'].groupby([node['Id'],node['Label']]).agg({'Weight':np.sum}).sort_values('Weight',ascending = False)
eresult = edge.sort_values('Weight',ascending = False)
nresult.to_csv('node.csv',index = False)
eresult.to_csv('edge.csv',index = False)

到此,相信大家對“Python如何爬取哈利波特小說”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI