我可以在python中使用xpath选择单个值,但是如何将几个单独的xpath连接起来以获得一个值呢?
以下是html源代码(r.content
)的示例片段:
<div class="members">
<h2>Members</h2>
<div class="member">
<span title="Last Online: 2017-02-20 22:37:42" data-time="2017-02-20T22:37:42Z">
<span class="profile-link">
<a href="/account/view-profile/KonterBolet">
<img class="achievement" src="36.png" alt="Completed 36" title="Completed 36">KonterA</a>
</span>
<span class="memberType">Leader</span>
</span>
</div>
<div class="member">
<span title="Last Online: 2017-02-19 11:28:20" data-time="2017-02-19T11:28:20Z">
<span class="profile-link hasTwitch twitchOffline" data-twitch-user="mardok_tv">
<a href="/account/view-profile/mardok">
<img class="achievement" src="35.png" alt="Completed 35" title="Completed 35">mardok</a>
<a class="twitch" href="//www.twitch.tv/mardok_tv" target="_blank" title="Offline"></a>
</span>
<span class="memberType">Officer</span>
</span>
</div>
</div>
我使用pythonrequests
来获取内容,lxml
来解析它
第一个xpath:acc = webContent.xpath("//span/a[contains(@href,'account/view-profile')]/text()")
结果:['konterA', 'mardok']
第二个xpath:twitch = webContent.xpath('//span/@data-twith-user')
结果:['mardok_tv']
第三个xpath:lastOnline = webContent.xpath('//span/@data-time')
结果:['2017-02-20T22:37:42Z','2017-02-19T11:28:20Z']
如何将这三者结合起来得到这样的结果:[['konterA','','2017-02-20T22:37:42Z'],['mardok','mardok_tv','2017-02-19T11:28:20Z']
考虑分析同一父级下的所有项,在顶层
xpath
上迭代。如果不存在attrib/element值,则使用XPath的concat()
返回一个空长度的字符串''
。下面还使用XPath的normalize-space()
从值中删除换行符和回车符。在我们叫它们
first_list, second_list and third_list
。 将second_list
修改为:之后,请执行以下操作:
^{pr2}$这应该以同样的方式给您一个元组列表。在
相关问题 更多 >
编程相关推荐