[PYTHON Tutorial] extract the article abstract

Source: Internet
Author: User
In the blog system's article list, in order to more effectively present the article content, so that readers can select to read more specifically, the article title and abstract are usually provided at the same time. In the blog system's article list, in order to more effectively present the article content, so that readers can select to read more specifically, the article title and abstract are usually provided at the same time.

The content of an article can be in plain text format, but in today's popular network, it is mostly in HTML format. Regardless of the format, the abstract is generally the content at the beginning of the article, which can be extracted according to the specified number of words.

Plain text summarization

First, we extract the plain text Abstract. the plain text document is a long string and can easily extract the abstract:

#! /Usr/bin/env python #-*-coding: UTF-8-*-"Get a summary of the TEXT-format document" "def get_summary (text, count ): u "Get the first 'count' characters from 'text' >>> text = u'welcome this is an article about Python '>>> get_summary (text, 12) = u'welcome this is an article 'True "assert (isinstance (text, unicode) return text [0: count] if _ name _ = '_ main _': import doctestdoctest. testmod ()

HTML abstract

HTML documents contain a large number of tags (for example,

And so on), these characters are Mark commands, and usually appear in pairs, simple text truncation will destroy the HTML document structure, and thus lead to improper display of the abstract in the browser.

While following the HTML document structure, you must parse the HTML document and intercept the content. In Python, you can use the standard library HTMLParser.

The simplest abstract extraction function is to extract only native text inside the tag while ignoring the HTML tag. Python implementation similar to this function is as follows:

#!/usr/bin/env python# -*- coding: utf-8 -*-"""Get a raw summary of the HTML-format document"""from HTMLParser import HTMLParserclass SummaryHTMLParser(HTMLParser):"""Parse HTML text to get a summary>>> text = u'

Hi guys:

This is a example using SummaryHTMLParser.

'>>> parser = SummaryHTMLParser(10)>>> parser.feed(text)>>> parser.get_summary(u'...')u'

Higuys:Thi...

'"""def __init__(self, count):HTMLParser.__init__(self)self.count = countself.summary = u''def feed(self, data):"""Only accept unicode `data"""`assert(isinstance(data, unicode))HTMLParser.feed(self, data)def handle_data(self, data):more = self.count - len(self.summary)if more > 0:# Remove possible whitespaces in `data`data_without_whitespace = u''.join(data.split())self.summary += data_without_whitespace[0:more]def get_summary(self, suffix=u'', wrapper=u'p'):return u'<{0}>{1}{2} '.format(wrapper, self.summary, suffix)if __name__ == '__main__':import doctestdoctest.testmod()

The above is the [PYTHON Tutorial] to extract the content of the article abstract. For more information, please follow the PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.