如何使用 LWP 获取网页的开头? [英] How do I fetch just the beginning of a Web page with LWP?

查看:45
本文介绍了如何使用 LWP 获取网页的开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道在 GET 或 POST 请求中只获取 50% 的网页的最佳方法吗?我获取的网页需要 10 到 20 秒才能完全加载,而且我只需要从页面开头过滤几行即可.

Does anyone know the best way to fetch just 50% of the Web page on a GET or POST request? The Web page I fetch takes me 10, 20 seconds to completely load, and I only need to filter just a few lines from the beginning of the page.

推荐答案

use 5.010;
use strictures;
use LWP::UserAgent qw();

my $content;
LWP::UserAgent->new->get(
    $url,
    ':content_cb' => sub {
        my ($chunk, $res) = @_;
        state $length = $res->header('Content-Length');
        $content .= $chunk;
        die if length($content) / $length > 0.5;
    },
);

这篇关于如何使用 LWP 获取网页的开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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