2012-03-11 54 views
0

在Perl中,使用模塊WWW::Mechanize(必需,而不是其他模塊),是否有可能從字符串變量,而不是URL「解析」文檔?Perl WWW ::從字符串變量機械化

我的意思,而不是

 $mech->get($url); 

做這樣的事情

 $html = '<html...'; 
     $mech->???($html); 

可能嗎?

回答

1

明白了:

  $mech->get(0); 
      $mech->update_html('<html>...</html>'); 

它的工作原理!

+2

什麼傻的接口。難怪我無法在文檔中找到它。 – singingfish 2012-03-14 09:01:45

-2

不是。您可以嘗試使用$mech->response獲取HTTP::Response對象,然後使用該對象的content方法將內容替換爲您自己的字符串。但是你必須調整所有的消息頭,它會變得非常混亂。

你想要做什麼? WWW::Mechanize提供的formsimages等方法基於其他模塊,代碼相當簡單。

+0

我需要分析其他程序獲取並作爲輸入傳入的HTML代碼。 – 2012-03-12 02:09:13

1

您可以將數據寫入磁盤,然後以通常的方式獲取()。事情是這樣的:

#!/usr/bin/env perl 

use strict; 
use warnings; 

use File::Temp; 
use URI::File; 
use WWW::Mechanize; 

my $data = '<html><body>foo</body></html>'; 

# write the data to disk 
my $fh = File::Temp->new; 
print $fh $data; 
$fh->close; 

my $mech = WWW::Mechanize->new; 
$mech->get(URI::file->new($fh->filename)); 

print $mech->content; 

打印:<HTML> <體>富< /身體> </HTML >

+0

我只是使用File :: Temp將數據寫入磁盤並獲得發送機械化使用$ mech-> get(URI :: file-> new($ tempfilename)) – singingfish 2012-03-12 09:27:10

+0

@singingfish非常感謝您指出出。我相應地更新了我的示例。很好地將代碼降下來。 – oalders 2012-03-12 17:03:21