Perl WWW :: Mechanize :: Firefox超时实现 [英] Perl WWW::Mechanize::Firefox timeout implementation

查看:109
本文介绍了Perl WWW :: Mechanize :: Firefox超时实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WWW :: Mechanize :: Firefox以及Firefox中的MozRepl插件.该代码可以正常工作,可以通过向网站发送HTTP GET请求来获取其内容.

I am using WWW::Mechanize::Firefox along with MozRepl plugin in Firefox. The code works properly to fetch content from sites by sending them an HTTP GET request.

我正在浏览一个URL列表,并向每个URL发送一个HTTP GET请求.

I am going through a list of URLs and sending an HTTP GET Request to each of them.

但是,如果请求挂在特定的URL上,它将保持等待状态.

However, if the request hangs on a particular URL, it keeps waiting.

请注意,我指的是在某些网页内容仍悬而未决的情况下加载一部分网页内容的情况.如果网页从第三方站点加载了很多内容,并且如果其中一种资源(例如图像)无法加载,则浏览器会一直等待它.

Please note that I am referring to cases where a part of the web page content is loaded while some of the content is still pending. It happens in cases where a web page loads a lot of content from third party sites and if one of the resources (an image for instance) could not be loaded, the browser keeps waiting for it.

我希望请求在"n"秒后超时,以便我可以从列表中读取下一个URL并继续执行代码.

I want the request to timeout after 'n' seconds so that I can read the next URL from the list and continue with the code execution.

在WWW :: Mechanize perl模块中,构造函数支持timeout选项,如下所示:

In WWW::Mechanize perl module, the constructor supported the timeout option as shown below:

$mech=WWW::Mechanize->new(timeout => 10);

但是,我在Perl模块的文档中找不到类似的选项,WWW :: Mechanize :: Firefox在这里:

However, I could not find a similar option in the documentation for the Perl Module, WWW::Mechanize::Firefox here:

http://metacpan.org/pod/WWW::Mechanize::Firefox

我尝试过:

$mech=WWW::Mechanize::Firefox->new(timeout => 10);

但是我认为它不起作用,因为仍然有一些请求被挂起的站点.

But I think it does not work as there are still some sites for which the request hangs.

推荐答案

WWW::Mechanize::Firefox使用Mozrepl连接Firefox浏览器,因此您无需声明超时参数,因为Firefox将等待页面加载.

WWW::Mechanize::Firefox uses Mozrepl to connect with Firefox browser, so you don't need to declare timeout parameter, because Firefox will wait for page load.

如果要检查网站是否真正满载,则应检查是否存在所需的元素(例如div):

If you want to check if site is really fully loaded, you should check that the element that you want (e.g. div) is present:

while (!$mech->is_visible( xpath => '//div[@class="myDivClassAtHtml"]')) {
          sleep 1;
}; 
# do something with your page

这篇关于Perl WWW :: Mechanize :: Firefox超时实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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