如何通过子项获取xpath元素子

时间:2017-07-27 18:19:28

标签: python xpath lxml

我的xml文件是这样的:

<div class = 'single-review'>
    <div class='review-header' data-id = 'aaabbb'></div>
    <div class='review-content'>
        <span class = 'name'>Alex</span>
        I'd like it
    </div>
</div>
<div class = 'single-review'>
    <div class='review-header' data-id = 'bbbccc'></div>
    <div class='review-content'>
        <span class = 'name'>Bob</span>
        I don't like it
    </div>
</div>

我想使用lxml来获取单一评论的所有元素并逐一进行分析:

single_reviews = all_data.xpath("//div[@class='single-review']")
for single_review in single_reviews:
     id = single_review.xpath("//div[@class='review-header']/@data-id")

但是它总是返回给我文件中的所有ID,但不是一个循环ID。如何解决问题? THX!

0 个答案:

没有答案