我正在尝试解析以下网页代码。 我能够让用户使用 xpath,但我无法使用 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()')
最佳答案
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']
关于python - lxml xpath 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34033622/