【问题标题】:How can I extract all links from the page excluding one using Perl's WWW::Mechanize?如何从页面中提取所有链接,不包括使用 Perl 的 WWW::Mechanize 的链接?
【发布时间】:2011-02-01 03:48:05
【问题描述】:

我正在尝试使用WWW::Mechanize 使用find_all_links() 方法从HTML 页面中提取一些链接。它支持匹配这些条件:

  • 文字
  • text_regex
  • 网址
  • url_regex
  • url_abs
  • url_abs_regex
    ...

如何提取所有链接除了包含文本“xyz”的链接?

【问题讨论】:

    标签: perl www-mechanize


    【解决方案1】:

    您可以使用'text_regex' 条件:

    $mech->find_all_links(text_regex => qr/^(?!xyz$).*$/);
    

    请参阅perldoc perlre 了解更多关于负前瞻断言的信息。

    【讨论】:

      【解决方案2】:

      为什么不获取所有链接,然后使用“grep”跳过不需要的链接?

      【讨论】:

      • 为什么不用问题来回答问题?
      • 这种方法还可以。在我的脚本中,find_all_links() 的参数是动态创建的。我发现使用这些参数更容易,没有任何特殊情况
      猜你喜欢
      • 2012-10-20
      • 1970-01-01
      • 2011-09-20
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2012-02-04
      • 2019-06-20
      相关资源
      最近更新 更多