如何用Perl提取HTML标题? [英] How do I extract an HTML title with Perl?
问题描述
像,让我说我有这样的HTML页面:
< html>< head>< title> TEST< ; /标题>< /头>< / HTML>
然后在Perl中我想这样做:
$ q - > H1(东西);
如何动态地将'something'替换为< title>标记中包含的内容?
我会使用 pQuery 。它可以像jQuery一样工作。
你可以这样说:
use pQuery;
my $ page = pQuery(http://google.com/);
my $ title = $ page-> find('title');
表示标题是:,$ title-> html;
替换东西类似:
$ title-> html('New Title');
表示整个google.com与我的新标题是:,$ page-> html;
您可以将HTML字符串传递给 pQuery
构造函数,它听起来像是你想要做的。
Is there a way to extract HTML page title using Perl? I know it can be passed as a hidden variable during form submit and then retrieved in Perl that way but I was wondering if there is a way to do this without the submit?
Like, lets say i have an HTML page like this:
<html><head><title>TEST</title></head></html>
and then in Perl I want to do :
$q -> h1('something');
How can I replace 'something' dynamically with what is contained in <title> tags?
I would use pQuery. It works just like jQuery.
You can say:
use pQuery;
my $page = pQuery("http://google.com/");
my $title = $page->find('title');
say "The title is: ", $title->html;
Replacing stuff is similar:
$title->html('New Title');
say "The entirety of google.com with my new title is: ", $page->html;
You can pass an HTML string to the pQuery
constructor, which it sounds like you want to do.
Finally, if you want to use arbitrary HTML as a "template", and then "refine" that with Perl commands, you want to use Template::Refine.
这篇关于如何用Perl提取HTML标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!