Package: transmogrify.htmlcontentextractor
Introduction============Helpful transmogrifier blueprints to extract text or html out of html content.transmogrify.htmlcontentextractor.auto~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~This blueprint has a clustering algorithm that tries to automatically extract the content from the HTML template.This is slow and not always effective. Often you will need to input your own template extraction rules.In addition to extracting Title, Description and Text of items the blueprint will outputthe rules it generates to a logger with the same name as the blueprint.Setting debug mode on templateauto will give you details about the rules it uses. ::...DEBUG:templateauto:'icft.html' discovered rules by clustering on 'http://...'Rules:text= html //div[@id = "dal_content"]//div[@class = "content"]//ptitle= text //div[@id = "dal_content"]//div[@class = "content"]//h3Text:TITLE: ...MAIN-10: ...MAIN-10: ...MAIN-10: ...Options-------conditionTAL Expression to control use of this blueprintdebugdefault is ''transmogrify.htmlcontentextractor~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~This blueprint extracts out title, description and body from htmleither via xpath, TAL or by automatic cluster analysisRules are in the form of ::(title|description|text|anything) = (text|html|optional|tal) ExpressionWhere expression is either TAL or XPathFor example ::[template1]blueprint = transmogrify.htmlcontentextractortitle       = text //div[@class='body']//h1[1]_delete1    = optional //div[@class='body']//a[@class='headerlink']_delete2    = optional //div[contains(@class,'admonition-description')]description = text //div[contains(@class,'admonition-description')]//p[@class='last']text        = html //div[@class='body']Note that for a single template e.g. template1, ALL of the XPaths need to match otherwisethat template will be skipped and the next template tried. If you'd like to make itso that a single XPath isn't nessary for the template to match then use the keyword `optional` or `optionaltext`instead of `text` or `html` before the XPath.When an XPath is applied within a single template, the HTML it matches will be removed from the page.Another rule in that same template can't match the same HTML fragment.If a content part is not useful (e.g. redundant text, title or description) it is a way to effectively remove that HTMLfrom the content.To help debug your template rules you can set debug mode.For more information about XPath see- http://www.w3schools.com/xpath/default.asp- http://blog.browsermob.com/2009/04/test-your-selenium-xpath-easily-with-firebug/HTMLContentExtractor====================This blueprint extracts out fields from html either via xpath rules or by automatic clusteranalysistransmogrify.htmlcontentextractor---------------------------------You can define a series of rules which will get applied to the to the '_text'of the input item. The rules use a XPATH expression or a TAL expression toextract html or text out of the html and adds it as key to the outputted item.Each option of the blueprint is a rule of the following form ::(N-)field = (optional)(text|html|delete|optional) xpathOR(N-)field = (optional)tal tal-expression"field" is the attribute that will be set with the results of the xpath"format" is what to do with the results of the xpath. "optional" means the sameas "delete" but won't cause the group to not match. if the format is delete or optionalthen the field name doesn't matter but will still need to be unique"xpath' is an xpath expressionIf the format is 'tal' then instead of an XPath use can use a TAL expression. TAL expressionis evaluated on the item object AFTER the XPath expressions have been applied.For example ::[template]blueprint = transmogrify.htmlcontentextractortitle = text //div[@class='body']//h1[1]_permalink = text //div[@class='body']//a[@class='headerlink']_text = html //div[@class='body']_label = optional //p[contains(@class,'admonition-title')]description = optional //div[contains(@class,'admonition-description')]/p[@class='last']/text()_remove_useless_links = optional //div[@id = 'indices-and-tables']mimetype = tal string:text/htmltext = tal python:item['_text'].replace('id="blah"','')You can delete a number of parts of the html by extracting content to fields such as _permalink and _label.These items won't get used be set used to set any properties on the final content so are effective as a meansof deleting parts of the html.TAL expressions are evaluated after XPath expressions so we can post process the _text XPath to produce a textstripped of a certain id.N is the group number. Groups are run in order of group number. Ifany rule doesn't match (unless its marked optional) then the next groupwill be tried instead. Group numbers are optional.Instead of groups you can also chain several blueprints togeather. The blueprintwill set '_template' on the item. If another blueprint finds the '_template' key in an itemit will ignore that item.The '_template' field is the remainder of the html once all the content selected by theXPATH expressions have been applied.transmogrify.htmlcontentextractor.auto--------------------------------------This blueprint will analyse the html and attempt to discover the rules to extract out thetitle, description and body of the html.If the logger output is in DEBUG mode then the XPaths used by the auto extrator will be outputto the logger.Changelog=========1.0 (2012-04-18)----------------- include datetime in tal expression. [djay]- fix bug in drop_tree when removing html [djay]- better logging [djay]- better handling of text nodes [djay]- added iterating accention of a text node in order uniquifiy [aterry]1.0b5 (2011-06-29)------------------- include docs- now can use TAL expressions1.0b4 (2011-02-06)------------------- handle '/text()' in  xpaths- new 'optionaltext' rule format1.0b3 (2010-12-13)------------------- simpler autogenerated xpath- better logging1.0b2 (2010-11-09)------------------- Put condition on autofinder so can be turned off1.0b1 (2010-11-03)------------------- ignore already found items. better debug["Dylan Jay"]- skip templates if item already parsed["Dylan Jay"]- print automaticly found XPaths["Dylan Jay"]- make text fields strip tail text["Vitaliy Podoba"]1.0dev (2010-03-22)-------------------- split the auto templatefinder out to it's own blueprint["Dylan Jay"]