如何向 WWW::Mechanize 添加进度条?
我有以下代码:
$mech->get($someurl, ":content_file" => "$i.flv");
所以我获取 url 的内容并将其保存为 flv 文件。我想每秒打印出剩余的下载量。在 WWW::Mechanize 中有什么方法可以实现这一点吗?
I have the following code:
$mech->get($someurl, ":content_file" => "$i.flv");
So I'm getting the contents of a url and saving it as an flv file. I'd like to print out every second or so how much of the download is remaining. Is there any way to accomplish this in WWW::Mechanize?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WWW::Mechanize
表示get
方法是 LWP::UserAgentget
的“行为良好”重载。查看 LWP::UserAgent 的文档,您可以提供一个content_cb
键,该键会在下载文件的每个块中调用:WWW::Mechanize
says that theget
method is a "well-behaved" overload of LWP::UserAgentget
. Looking at the docs for LWP::UserAgent, you can provide acontent_cb
key which is called with each chunk of the downloaded file:非常感谢彼得·科瓦奇的回答引导我找到正确的答案。结果比我预想的要复杂一些,所以我决定(恐怖)回答我自己的问题。
正如 Peter 所示,我可以像这样设置回调:
但现在我无法使用 :content_file 值保存内容,因为我只能选择两者之一。回调函数传递了数据,我最终将其写入文件。
我还得到一个响应对象,其中包含弗里多指出的内容的总大小。因此,通过保留迄今为止收到的内容的运行总数并将其除以总内容,我可以找出已下载内容的百分比。这是完整的回调函数:
我希望对某人有所帮助。
Many thanks to Peter Kovacs' answer for leading me to the correct answer. It turned out to be a bit more elaborate than I'd expected though so I decided to (horror) answer my own question.
As Peter showed, I can set a callback like so:
But now I can't save the content using the :content_file value, because I can only choose one of the two. The callback function gets passed the data, and I ended up writing that to a file instead.
I also get a response object which contains the total size of the content as friedo pointed out. So by keeping a running total of content received so far and dividing it by the total content I can find out what percent of the content has been downloaded. Here's the full callback function:
I hope that helps someone.