使用WWW :: Mechanize或LWP :: UserAgent获取文本/事件流Web响应

时间:2016-06-10 19:23:04

标签: perl www-mechanize lwp-useragent event-stream

我正在使用WWW::Mechanize来获取包含Google地图窗口小部件的网页,该窗口小部件从类型为text / event-stream的单个响应中接收常量数据。

这种响应就像是来自服务器的永无止境的响应,它不断地返回更新的数据以使小部件工作。

我正试图找出如何从Perl中读取确切的响应。使用类似的东西:

my $mech = WWW::Mechanize->new;

# Do some normal GET and POST requests to authenticate and set cookies for the session

# Now try to get that text/event-stream response

$mech->get('https://some.domain.com/event_stream_page');

但这不起作用,因为反应永远不会结束。

每次服务器更新流时,如何发出请求并开始阅读响应并对该数据执行某些操作?

1 个答案:

答案 0 :(得分:4)

找到了一种方法。使用handler from LWP继承WWW::Mechanize

$mech->add_handler (
    'response_data',
    sub {
        my ($response, $ua, $h, $data) = @_;
        # Your chunk of response is now in $data, do what you need
        # If you plan on reading an infinite stream, it's a good idea to clean the response so it doesn't grow infinitely too!
        $response->content(undef);
        # Important to return a true value if you want to keep reading the response!
        return 1;
    },
);