如何用Perl提取HTML标题? [英] How do I extract an HTML title with Perl?

查看:83
本文介绍了如何用Perl提取HTML标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使用Perl来提取HTML页面标题?我知道它可以在表单提交时作为隐藏变量传递,然后以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 构造函数,它听起来像是你想要做的。

最后,如果你想使用任意的HTML作为模板,然后用Perl精炼命令,您希望使用 Template :: Refine


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆