选择并反转标记(html2text)网页片段。
frag2text的Python项目详细描述
frag2文本
Markdown为您提供纯文本的html,并html2text反转 过程。如果您想要的纯文本版本的特定节 一个网页(一个html片段),您通常会选择 (解析)首先,然后生成标记文本以保留一些 格式化。
我把frag2text制作成:
- 通过css选择器或xpath轻松选择网页片段 表达式
- 获取片段的纯文本,并保留 以后与降价一起使用
- 不向另一个程序(如lynx-dump)提供外壳
- 不直接分析HTML或文本
- 使用html5lib进行健壮的解析
- 有一个简单的易于维护的python模块(封装 将解析器、treebuilders和 序列化程序,保持简单-这不需要 美组)
这个问题我已经解决了一段时间 不同的方式。它看起来微不足道,但很快就会变得可笑。如果你 有什么建议或者想和别人分享你的经验 工具,请告诉我。
安装
$ pip install frag2text
用法
Python
>>>fromfrag2textimportfrag2text>>>help(frag2text)Helponfunctionfrag2textinmodulefrag2text:frag2text(endpoint,stype,selector,clean=False,raw=False,verbose=False)returnsMarkdowntextofselectedfragment.Args:endpoint:URL,file,orHTMLstringstype:{'css'|'xpath'}selector:CSSselectororXPathexpressionReturns:MarkdowntextOptions:clean:cleansfragment(lxml.html.cleandefaults)raw:returnsrawHTMLfragmentverbose:showhttpstatus,encoding,headers
外壳
$ frag2text.py -h usage: frag2text.py [-h][-c][-r][-v] endpoint {css,xpath} selector reverse Markdown (html2text) HTML fragments. positional arguments: endpoint URL, file, or HTML string {css,xpath} fragment selector type selector CSS select statement or XPath expression optional arguments: -h, --help show this help message and exit -c, --clean clean fragment (lxml.html.clean defaults) -r, --raw output raw fragment -v, --verbose print status, encoding, headers
示例
Python
fromfrag2textimportfrag2textinfo=frag2text('http://wikipedia.org/wiki/Amanita','css','.infobox')
外壳
$ frag2text.py "<ht?+><borkt><h1>hello" xpath //h1 ... # hello
css选择
$ frag2text.py http://wikipedia.org/wiki/Amanita css .infobox _Amanita_ --- ![Fliegenpilz-1.jpg](//upload.wikimedia.org/wikipedia/commons/thumb/d/d1 /Fliegenpilz-1.jpg/230px-Fliegenpilz-1.jpg) _[Amanita muscaria](/wiki/Amanita_muscaria)_ Albin Schmalfuß, 1897[Scientific classification](/wiki/Biological_classification) Kingdom: |[Fungi](/wiki/Fungi) Division: |[Basidiomycota](/wiki/Basidiomycota) Class: |[Agaricomycetes](/wiki/Agaricomycetes) Order: |[Agaricales](/wiki/Agaricales) Family: |[Amanitaceae](/wiki/Amanitaceae) Genus: | _**Amanita**_ [Pers.](/wiki/Christian_Hendrik_Persoon)(1794)[Type species](/wiki/Type_species) _[Amanita muscaria](/wiki/Amanita_muscaria)_ ([L.](/wiki/Linnaeus))[Lam.](/wiki/Lam.)(1783)[Diversity](/wiki/Biodiversity)[c.600 species](/wiki/List_of_Amanita_species)
xpath表达式
$ frag2text.py http://en.wikipedia.org/wiki/Amanita xpath '//p[1]' The [genus](/wiki/Genus) _**Amanita**_ contains about 600[species](/wik i/Species) of [agarics](/wiki/Agarics) including some of the most [toxic ](/wiki/Toxic) known [mushrooms](/wiki/Mushrooms) found worldwide, as we ll as some well-regarded edible species. This genus is responsible for a pproximately 95% of the fatalities resulting from [mushroom poisoning](/ wiki/Mushroom_poisoning), with the [death cap](/wiki/Death_cap) accounti ng for about 50% on its own. The most potent toxin present in these mush rooms is α[-amanitin](/wiki/%CE%91-amanitin).
发布历史
0.0.5(2015-02-18)
- 处理xpathevalerror、selector或syntaxerror,但未找到任何结果。
- 所选片段的联接列表,只返回第一个。 <>不要过早退出错误。
0.0.1(2015-01-14)
- 好像有用!