我有以下html代码,可用来解析评分/文本等。 我该如何使用lxml和tree遍历div“ wrap”下面的所有divs类中包含“ posting item”的
?在下面,我同时选择了所有帖子div
forumposts = tree.xpath("//div[@class='wrap']//div[contains(@class, 'posting item')]")
# here i want to iterate through posting items
# so i should have 1 text/rating to process in the parse function
for post in forumposts:
parse(post)
HTML:
<div class="wrap">
<div class="posting item theme-international" data-postingid="1035091361">
<div class="thread">
<div class="js-ratings ratings">
<div class="js-ratings-counts ratings-counts" data-closable-
target="ratinglog-1-1035091361"
onclick="ForumLoader.toggleRatinglog(1035091361, 1)">
<span class="js-ratings-negative-count ratings-negative-
count">6</span>
<span class="js-ratings-positive-count ratings-positive-
count">7</span>
</div>
</div>
<div class="text">
<a href="xyz" rel="nofollow">
<strong/>
<span>Posting text 1 </span>
</a>
</div>
</div>
</div>
<div class="posting item theme-international" data-postingid="1035091361">
<div class="thread">
<div class="js-ratings ratings">
<div class="js-ratings-counts ratings-counts" data-closable-
target="ratinglog-1-1035091361"
onclick="ForumLoader.toggleRatinglog(1035091361,
1)">
<span class="js-ratings-negative-count ratings-negative-
count">1</span>
<span class="js-ratings-positive-count ratings-positive-
count">11</span>
</div>
</div>
<div class="text">
<a href="xyz" rel="nofollow">
<strong/>
<span>Posting text 2</span>
</a>
</div>
</div>
</div>
</div>
答案 0 :(得分:0)
我不清楚你想要什么,尼科。是这个吗?
>>> from lxml import etree
>>> parser = etree.HTMLParser()
>>> tree = etree.parse(open('nico.htm'), parser)
>>> for s in tree.xpath('//div[@class="wrap"]//div[@class="text"]//span'):
... s.text
...
'Posting text 1 '
'Posting text 2'