2015-12-02 58 views
2

我试图解析为其网页代码如下。 我能够得到使用xpath的用户,但我无法使用xpath得到他们的分数任何想法我在这里做错了吗?LXML的XPath不工作

import requests 
from lxml import html 

internsHack = 'https://doselect.com/hackathon/inmobi-internshack/leaderboard' 

page = requests.get(internsHack) 
tree = html.fromstring(page.content) 

users = tree.xpath('//div[@class="md-list-item-text"]/h2/a/text()') 
score = tree.xpath('//div[@class="points-score"]/ng-pluralize/text()') 

回答

2

HTML源代码段:

<div class="points-score"> 
    <ng-pluralize count="200" 
          when="{'0': '{} point', 
           'one': '{} point', 
           'other': '{} points'}"> 
</div> 

获取count属性值,而不是text():然后

//div[@class="points-score"]/ng-pluralize/@count 

score变量将具有以下值:

['200', '198', '198', '197', '197', '197', '196', '195', '194', '194'] 
+0

你的答案是正确的,但很想知道你是怎么计算计数,当我在铬200点的开发工具寻找这个属性是一个文本项 –

+0

@NickLoach你在“源”,在浏览器的开发者工具看是浏览器呈现的页面,它可能与初始页面严重不同。你使用'requests'获得的是最初的未渲染页面 - 这是你应该检查的内容。希望有所帮助。 – alecxe

+0

感谢您的解释我检查请求内容现在它的难以辨认,但你对count属性是正确的 –