python網頁自動摘要和關鍵詞提取

來源:互聯網
上載者:User

最近準備用django寫一個CMS系統,把單位的網頁遷移過去,在網上找了一個部落格系統,但是不太滿意,摘要和tags都需要自己輸入,目標就是把他們實現自動化。

之前搞爬蟲接觸過類似的庫,readability,goose等都可以實現文本摘要,jieba具有提取tags的功能。

goose主要作用是根據網址提取文本和標題。主要用的庫有urllib2,beautifulsoup;有些網頁用goose提取不到本文,只能獲得標題,因此用在內容也上比較好,目錄頁、首頁效果不太好。

用法如下:


from goose import Goosefrom goose.text import StopWordsChineseg = Goose({'browser_user_agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ','stopwords_class': StopWordsChinese})article = g.extract(url=url)url = 'http://blog.csdn.net/u011617072/article/details/12624855'print article.titleprint article.cleaned_text[:]

 

goose其實不怎麼滿足需求,簡單點的還有html2text,或者用htmlparse,甚至簡單粗暴的Regex。

# -*- coding: utf-8 -*-from HTMLParser import HTMLParserclass MLStripper(HTMLParser):    def __init__(self):        self.reset()        self.fed = []    def handle_data(self, d):        self.fed.append(d)    def get_data(self):        return ''.join(self.fed)def strip_tags(html):    s = MLStripper()    s.feed(html)    return s.get_data()

提取關鍵詞用jieba比較簡單,預設提供TF/IDF權重最高的20個詞。

import jiebaimport jieba.analysedef extract_tags(content,topk):    content = content.strip()    tags=jieba.analyse.extract_tags(content, topK=topk)    return ','.join(tags)



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.