Python爬取十篇新聞統(tǒng)計(jì)TF-IDF
統(tǒng)計(jì)十篇新聞TF-IDF
統(tǒng)計(jì)TF-IDF詞頻,每篇文章的 top10 的高頻詞存儲為 json 文件
TF-IDF
TF-IDF(term frequency–inverse document frequency)是一種用于資訊檢索與文本挖掘的常用加權(quán)技術(shù)。TF-IDF是一種統(tǒng)計(jì)方法,用以評估一字詞對于一個(gè)文件集或一個(gè)語料庫中的其中一份文件的重要程度。字詞的重要性隨著它在文件中出現(xiàn)的次數(shù)成正比增加,但同時(shí)會隨著它在語料庫中出現(xiàn)的頻率成反比下降。TF-IDF加權(quán)的各種形式常被搜索引擎應(yīng)用,作為文件與用戶查詢之間相關(guān)程度的度量或評級。除了TF-IDF以外,互聯(lián)網(wǎng)上的搜索引擎還會使用基于連結(jié)分析的評級方法,以確定文件在搜尋結(jié)果中出現(xiàn)的順序。
假如一篇文件的總詞語數(shù)是100個(gè),而詞語“母牛”出現(xiàn)了3次,那么“母牛”一詞在該文件中的詞頻就是3/100=0.03。一個(gè)計(jì)算文件頻率(DF)的方法是測定有多少份文件出現(xiàn)過“母牛”一詞,然后除以文件集里包含的文件總數(shù)。所以,如果“母?!币辉~在1,000份文件出現(xiàn)過,而文件總數(shù)是10,000,000份的話,其逆向文件頻率就是log(10,000,000 / 1,000)=4。最后的TF-IDF的分?jǐn)?shù)為0.03 * 4=0.12。 —— [ 維基百科 ]
博主選擇的是chinadaily的十篇新聞.
1.使用http request請求
2.使用Beautiful Soup來抓取文章標(biāo)題和內(nèi)容
3.統(tǒng)計(jì)TF-IDF
4.保存到j(luò)son文件中
代碼塊
@requires_authorization #coding=utf-8 import requests import bs4 import sys import math import json reload(sys) sys.setdefaultencoding('utf-8') url_list = ['http://www.chinadaily.com.cn/china/2016-04/20/content_24701635.htm', 'http://www.chinadaily.com.cn/china/2016-04/20/content_24700746.htm', 'http://www.chinadaily.com.cn/china/2016-04/20/content_24681482.htm', 'http://www.chinadaily.com.cn/china/2016-04/19/content_24675530.htm', 'http://www.chinadaily.com.cn/china/2016-04/19/content_24675455.htm', 'http://www.chinadaily.com.cn/china/2016-04/19/content_24674074.htm', 'http://www.chinadaily.com.cn/china/2016-04/19/content_24655536.htm', 'http://www.chinadaily.com.cn/china/2016-04/18/content_24643685.htm', 'http://www.chinadaily.com.cn/china/2016-04/18/content_24636917.htm', 'http://www.chinadaily.com.cn/china/2016-04/15/content_24562198.htm' ] articles_title = [] articles_content = [] for pos,url in enumerate(url_list): r = requests.get(url) soup1 = bs4.BeautifulSoup(r.text) soup2 = bs4.BeautifulSoup(str(soup1.find_all(id="Title_e"))) articles_title.append(soup2.h1.string) mystr = "" soup3 = bs4.BeautifulSoup(str(soup1.find_all(id="Content"))) for x in soup3.find_all("p"): mystr = mystr + x.string str_p = "" contents = [] for pos,x in enumerate(mystr): if x == '.' or x == ',': if pos < (len(mystr) - 1) and mystr[pos+1] >= '0' and mystr[pos+1] <= '9': str_p = str_p + x elif str_p == "": continue else: contents.append(str_p) str_p = "" elif x == '(' or x == ')' or x == ' ' or x == '"' or x == '[' or x == ']' or x == '-': if str_p == "": continue else: contents.append(str_p) str_p = "" else: str_p = str_p + x articles_content.append(contents) Dict_idf = {} DictList = [] for content in articles_content: Dict_tf = {} for x in content: if not Dict_tf.has_key(x): Dict_tf[x] = 1.0 if not Dict_idf.has_key(x): Dict_idf[x] = 1.0 else: Dict_idf[x] += 1.0 else: Dict_tf[x] += 1.0 for k, v in Dict_tf.items(): Dict_tf[k] = v / len(content) DictList.append(Dict_tf) for k, v in Dict_idf.items(): Dict_idf[k] = math.log(float(len(url_list)) / v) for pos,x in enumerate(DictList): for k,v in x.items(): DictList[pos][k] = v*Dict_idf[k] DictList[pos] = sorted(x.iteritems(), key=lambda d: d[1], reverse=True) """ [ [ article_titile:"XXXX" [ { word:"hello" value:3.5 } { word:"hello" value:3.5 } { word:"hello" value:3.5 } ... ] ] ] """ data = [] for pos in range(10): data2=[] data2.append("article_titile:") data2.append(articles_title[pos]) data2.append([{"word": k,"value":round(v,4)} for k,v in DictList[pos][:10]]) data.append(data2) # Writing JSON data with open('data.json', 'w') as f: json.dump(data, f)
使用json.cn查看數(shù)據(jù):
github地址:https://github.com/mqsee/learngit
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python 生成 -1~1 之間的隨機(jī)數(shù)矩陣方法
今天小編就為大家分享一篇Python 生成 -1~1 之間的隨機(jī)數(shù)矩陣方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08python3+RobotFramework環(huán)境搭建過程
之前用的python2.7+robotframework進(jìn)行的自動化測試,python3的還沒嘗試,今天嘗試了下,搭建環(huán)境的時(shí)候也是各種報(bào)錯,今天給大家分享下python3+RobotFramework環(huán)境搭建過程,感興趣的朋友一起看看吧2023-08-08flask路由分模塊管理及自定義restful響應(yīng)格式詳解
這篇文章主要為大家介紹了flask路由分模塊管理及自定義restful響應(yīng)格式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08使用python數(shù)據(jù)清洗代碼實(shí)例
這篇文章主要介紹了使用python數(shù)據(jù)清洗代碼實(shí)例,分享一下近期用python做數(shù)據(jù)清洗匯總的相關(guān)代碼,這里我們用到的python包有pandas、numpy、os等,需要的朋友可以參考下2023-07-07Python 實(shí)現(xiàn)文件讀寫、坐標(biāo)尋址、查找替換功能
這篇文章主要介紹了Python 實(shí)現(xiàn)文件讀寫、坐標(biāo)尋址、查找替換功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-09-09Python中使用pypdf2合并、分割、加密pdf文件的代碼詳解
這篇文章主要介紹了Python中使用pypdf2合并、分割、加密pdf文件的代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05PyTorch預(yù)訓(xùn)練的實(shí)現(xiàn)
這篇文章主要介紹了PyTorch預(yù)訓(xùn)練的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09