【问题标题】:Use of URI method in mechanize#link在mechanize中使用URI方法#link
【发布时间】:2014-05-21 05:54:12
【问题描述】:

我正在尝试从可用的标签中获取 URL。所以如果我的 html 包含

<a class= "classname" href="someurl.com"; title="title">some text</a>

如果我这样做

@url = link.search("a[@href]")

它返回我上面的 html。

如果我这样做.text,那么它会返回我Some text

但我想获得href url。我见过this link。但不知道如何实现它。

任何建议。

【问题讨论】:

    标签: ruby mechanize mechanize-ruby


    【解决方案1】:

    我认为 mechanize 使用nokogiri 来解析内容。因此,要获取属性的值,您可以使用attr 方法然后获取它的值,如下所示:

    link.search("a[@href]").attr('href').value
    

    【讨论】:

    • 其实大概应该是:link.at('a[href]')[:href]
    猜你喜欢
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 2017-04-21
    • 2012-07-10
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多