【问题标题】:get the value of href using lxml in html table在html表中使用lxml获取href的值
【发布时间】:2012-05-10 18:44:05
【问题描述】:

我有一个 html 表格。
在那张桌子上我有

<td>abc</td><a>www.abc.com</a>  

如何通过匹配 td 的值来获取链接值?

例如:如何使用 lxml 搜索 tect abc 获得 www.abc.com 的值?

【问题讨论】:

    标签: python html lxml


    【解决方案1】:

    a/text() 获取文本

    a/@href 获取 attr(在这种情况下为href

    UPD

    >>> from lxml import etree
    >>> etree.fromstring('<html><td>abc</td><a>www.abc.com</a></html>').xpath("//td/following-sibling::a/text()")
    ['www.abc.com']
    

    XPath 1.0

    【讨论】:

    • 是的,但是我如何通过搜索 abc 获得 www.abc.com? doc.xpath('/a/text()') 我已经添加了一些东西来获取相同文本的链接?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 2015-04-09
    • 2015-01-05
    • 2015-02-04
    • 1970-01-01
    相关资源
    最近更新 更多