在Python中获取两个标签之间的数据

问题描述 投票:0回答:2
<h3>
<a href="article.jsp?tp=&arnumber=16">
Granular computing based
<span class="snippet">data</span>
<span class="snippet">mining</span>
in the views of rough set and fuzzy set
</a>
</h3>

使用Python我想从锚标签中获取值,这应该是基于粗糙集和模糊集视图中的粒度计算的数据挖掘

我尝试使用 lxml

parser = etree.HTMLParser()
tree   = etree.parse(StringIO.StringIO(html), parser)                   
xpath1 = "//h3/a/child::text() | //h3/a/span/child::text()"
rawResponse = tree.xpath(xpath1)              
print rawResponse

并得到以下输出

['\r\n\t\t','\r\n\t\t\t\t\t\t\t\t\tgranular computing based','data','mining','in the view of roughset and fuzzyset\r\n\t\t\t\t\t\t\]
python web-scraping lxml
2个回答
3
投票

您可以使用

text_content
方法:

import lxml.html as LH

html = '''<h3>
<a href="article.jsp?tp=&arnumber=16">
Granular computing based
<span class="snippet">data</span>
<span class="snippet">mining</span>
in the views of rough set and fuzzy set
</a>
</h3>'''

root = LH.fromstring(html)
for elt in root.xpath('//a'):
    print(elt.text_content())

产量

Granular computing based
data
mining
in the views of rough set and fuzzy set

或者,要删除空格,您可以使用

print(' '.join(elt.text_content().split()))

获得

Granular computing based data mining in the views of rough set and fuzzy set

这是您可能会发现有用的另一个选项:

print(' '.join([elt.strip() for elt in root.xpath('//a/descendant-or-self::text()')]))

产量

Granular computing based data  mining in the views of rough set and fuzzy set

(请注意,它在

data
mining
之间留下了额外的空格。)

'//a/descendant-or-self::text()'
是更通用的版本
"//a/child::text() | //a/span/child::text()"
。它将遍历所有子代和孙代等。


1
投票
© www.soinside.com 2019 - 2024. All rights reserved.