【发布时间】:2016-04-22 03:36:35
【问题描述】:
我正在学习 xpath 并尝试使用 python lxml/html 获取具有特定节点属性的节点的值,例如(google playstore)。从下面的代码中,我想从节点“a”获取开发人员电子邮件值,属性“href”以“mailto:”开头。我的 python 代码 sn-p 返回应用程序名称,但开发人员电子邮件为空。谢谢
<html>
<div class="id-app-title" tabindex="0">Candy Crush Saga</div>
<div class="meta-info meta-info-wide">
<div class="title"> Developer </div>
<a class="dev-link" href="https://www.google.com/url?q=http://candycrush.com" rel="nofollow" target="_blank"> Visit website </a>
<a class="dev-link" href="mailto:candycrush@kingping.com"
rel="nofollow" target="_blank">candycrush@kingping.com </a> ##Interesting part here
</div>
</html>
Python 代码 (2.7)
def get_app_from_link(self,link):
start_page=requests.get(link)
#print start_page.text
tree = html.fromstring(start_page.text)
name = tree.xpath('//div[@class="id-app-title"]/text()')[0]
#developer=tree.xpath('//div[@class="dev-link"]//*/div/@href')
developer=tree.xpath('//div[contains(@href,"mailto") and @class="dev-link"]/text()')
print name,developer
return
【问题讨论】:
标签: python xpath python-requests lxml lxml.html