perl – 我应该如何使用Mojo :: UserAgent处理HTML META标签?
发布时间:2020-12-16 06:12:19 所属栏目:大数据 来源:网络整理
导读:我必须使用一些配置错误的Web服务器,因此我开始处理 HTML元标记以将信息反馈回Web用户代理对象.我在 Mojolicious尝试了各种各样的方法,并决定在响应中寻找“完成”事件.我的目标是使其对代码的其余部分几乎不可见,因此进程甚至不知道发生了这种情况. 尽管如
我必须使用一些配置错误的Web服务器,因此我开始处理
HTML元标记以将信息反馈回Web用户代理对象.我在
Mojolicious尝试了各种各样的方法,并决定在响应中寻找“完成”事件.我的目标是使其对代码的其余部分几乎不可见,因此进程甚至不知道发生了这种情况.
尽管如此,这并不适合我,因为我无法完全放下手指.除了process_meta_options中的特定代码之外,还有一种更为Mojolicious的方法吗?例如,Mojo::UserAgent get() with userdefined callback使用read事件,但我倾向于认为这可能会干扰事情.或者我可能只是过度思考它. use v5.20; use feature qw(signatures); no warnings qw(experimental::signatures); use Data::Dumper; use Mojo::UserAgent; my $ua = Mojo::UserAgent->new; my $tx = $ua->build_tx( GET => 'http://blogs.perl.org' ); $tx->res->on( finish => &;process_meta_options ); $tx = $ua->start( $tx ); say "At end,charset is ",$tx->res->content->charset; sub process_meta_options ( $res ) { $res ->dom ->find( 'head meta[charset]' ) # HTML 5 ->map( sub { my $content_type = $res->headers->header( 'Content-type' ); return unless my $meta_charset = $_->{charset}; $content_type =~ s/;.*//; $res->headers->header( 'Content-type',"$content_type; charset=$_->{charset}" ); } ); } 解决方法
我认为答案正是我提出的.我没有发现任何我更喜欢的东西.
use v5.20; use feature qw(signatures); no warnings qw(experimental::signatures); use Data::Dumper; use Mojo::UserAgent; my $ua = Mojo::UserAgent->new; my $tx = $ua->build_tx( GET => 'http://blogs.perl.org' ); $tx->res->on( finish => &;process_meta_options ); $tx = $ua->start( $tx ); say "At end,"$content_type; charset=$_->{charset}" ); } ); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |