【发布时间】:2021-02-13 14:30:37
【问题描述】:
我正在尝试找出为什么此代码在某些极端情况下会失败:
use HTML::TreeBuilder;
use Data::Dumper;
my $page = `curl -H "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" -L -A "Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9; PageThing http://pagething.com) Gecko/2008052906 Firefox/3.0" --compressed --silent --max-time 10 --location --connect-timeout 10 'weboost.com'`;
my $root = HTML::TreeBuilder->new_from_content($page);
my $title = $root->look_down( '_tag', 'title' );
print Dumper($title); # comes back as undef
my $page_title = $title->as_text;
print "BLA: $page_title\n";
我明白了:
Can't call method "as_text" on an undefined value at test.cgi line 28.
我已经确认<title> 存在:
<title>weBoost</title>
那为什么找不到呢?
【问题讨论】:
-
选择有此类问题的小页面并转储解析树 - 它可能会给您一个提示。
$root->dump(); # prints dump to STDOUT -
面对这个问题,我首先会转储 curl 检索到的文本并手动查看它。如果你已经这样做了,我很抱歉!
标签: perl