In the blog system article list, in order to more effectively present the content of the article, so that the reader more targeted choice of reading, usually also provides the title and summary of the article.
The content of an article can be in plain text format, but in today's web, more is HTML format. Regardless of the format, the summary is generally the beginning of the article content, you can follow the specified number of words to extract.
Plain Text Summary
First of all, we extract the Plain Text digest, the plain text document is a long string, it is easy to achieve digest extraction:
#!/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 = = U ' Welcome This is a ' True ' "" Assert (Isinstance (text, Unicode)) return text[0:count]if __name__ = = ' __main__ ': Import Doctestdoctest.testmod ()
HTML Summary
HTML documents contain a large number of markers (such as
When you follow the structure of the HTML document and intercept the content, you need to parse the HTML document. In Python, you can do this with the standard library htmlparser.
One of the simplest digest extraction functions is to omit HTML tags and extract only the native text inside the markup. The following is a python implementation similar to this feature:
#!/usr/bin/env python#-*-coding:utf-8-*-"" "Get a raw summary of the Html-format document" "from Htmlparser import HTML Parserclass Summaryhtmlparser (Htmlparser): "" "Parse HTML text to get a summary>>> Text = U ' <p>hi guys:</ P><p>this is a example using summaryhtmlparser.</p> ' >>> parser = Summaryhtmlparser (Ten) >> > parser.feed (text) >>> parser.get_summary (U ' ... ') u ' <p>Higuys:Thi...</p> ' "" 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}</{0}> '. Format (wrapper, self.summary, suffix) iF __name__ = = ' __main__ ': Import Doctestdoctest.testmod ()
The above is the "Python tutorial" extract the content of the article summary, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!