【问题标题】:python lxml xpath get the nodes attributes with specific string patternpython lxml xpath 获取具有特定字符串模式的节点属性
【发布时间】: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


    【解决方案1】:

    现在你使用的是标签div,而不是a:

    '//a[contains(@href,"mailto") and @class="dev-link"]/text()'

    另外,您的函数不返回项目。使用return 喜欢:

    def get_app_from_link(self,link)::
        # your code
        return name, developer
    

    【讨论】:

      猜你喜欢
      • 2012-12-23
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 2017-02-23
      • 2011-05-13
      • 2010-10-11
      • 1970-01-01
      • 2011-01-30
      相关资源
      最近更新 更多