【问题标题】:Extracting a line from a webpage using WWW::Mechanize module in perl使用 perl 中的 WWW::Mechanize 模块从网页中提取一行
【发布时间】:2012-12-28 03:23:30
【问题描述】:

这是我的网页示例数据

<hr>
<h4>This is Second line</h4>
Some Text Here<br>
Some More Text Here<br>

<h4>This is First line</h4>
Mem Capacity : 130.65 MB<br>
Mem Used : 74.52 MB<br>
Mem Available : 56.13 MB<br>
Mem Used Percentage : 57<br>

我正在使用下面的代码来提取类似的东西

Mem Capacity : 130.65 MB
Mem Used : 74.52 MB
Mem Available : 56.13 MB
Mem Used Percentage : 57

代码如下:

#!/usr/bin/perl
 use WWW::Mechanize;

 $mech = WWW::Mechanize->new();
 $url = 'some url';
 $result = $mech->get( $url );
 $content = $result->as_string();
 print $content;
 if($content =~ /Mem Capacity :([\d.]+)/)
 {
 $value = $1;
 print "Memory Capacity $value MB n";
 }

我没有得到任何输出。谁能告诉我哪里出错了?

【问题讨论】:

  • 你应该use strict; use warnings;。在这种情况下,如果您只有 HTML 来处理,我还建议使用 HTML 解析器而不是 WWW::Mechanize。
  • @squiguy 你能告诉我如何用 HTML 解析器实现相同的代码吗?
  • 这显然是这个问题的延续:stackoverflow.com/questions/14051191/… 我的问题仍然是一样的:你如何定义你正在寻找的行?

标签: perl perl-module www-mechanize


【解决方案1】:

试试这个 - if($content =~ /Mem\s*Capacity\s*:\s*(\d+(.\d+)?)/i )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 2012-06-10
    • 2011-02-01
    • 2015-11-30
    • 1970-01-01
    相关资源
    最近更新 更多