首页 > 解决方案 > 在使用 Scraper wget 下载图像之前查找文件大小

问题描述

我试图在我的 wget 实际下载之前获得图像/视频大小。有没有办法获取大小并将其保存到变量中?

我试图在网上找到这个,但是,我找不到任何解决方案。

标签: unixwget

解决方案


使用该--spider选项,然后将输出保存到文本文件:

$ wget --spider https://www.google.com/image.jpg > output.txt
$ wget --spider https://www.google.com/video.mp4 > output.txt

示例输出:

Spider mode enabled. Check if remote file exists.
--2016-09-16 14:23:42--  http://www.bbc.co.uk/
Resolving www.bbc.co.uk (www.bbc.co.uk)... 212.58.244.67, 212.58.246.91
Connecting to www.bbc.co.uk (www.bbc.co.uk)|212.58.244.67|:80... connected.
HTTP request sent, awaiting response...
  HTTP/1.1 200 OK
  Server: nginx
  Content-Type: text/html; charset=utf-8
  ...
Length: 171933 (168K) [text/html]  <---------- Right here is the file size
Remote file exists and could contain further links,
but recursion is disabled -- not retrieving.

然后使用awk读取第 10 行的第 2 个字来获取文件大小:

$ awk 'fileSize==10 {print $2}' output.txt

推荐阅读