我可以计算一个可供公众使用的网站的总大小吗?

22l*_*nly 4 websites wget file-size

假设我想下载所有公共页面或制作网站 www.psychocats.net 的离线数据库。现在如何在开始下载之前先计算网站的总大小?

小智 6

基于类似的问答 -在 wget-ing 之前将文件的文件大小获取到 wget?- 我制作了 bash shell 包装器脚本,它可以完全满足您的需求。:)

最新的代码库可以在 Github 上找到:

#!/bin/bash
# Info: https://github.com/mariomaric/website-size#readme

# Prepare wget logfile
log=/tmp/wget-website-size-log

# Do the spider magic
echo "### Crawling ${!#} website... ###"
sleep 2s
echo "### This will take some time to finish, please wait. ###"

wget \
  --recursive --level=inf \
  --spider --server-response \
  --no-directories \
  --output-file="$log" "$@"

echo "Finished with crawling!"
sleep 1s

# Check if prepared logfile is used
if [ -f "$log" ]; then
    # Calculate and print estimated website size
    echo "Estimated size: $(\
        grep -e "Content-Length" "$log" | \
        awk '{sum+=$2} END {printf("%.0f", sum / 1024 / 1024)}'\
    ) Mb"

    # Delete wget log file
    rm "$log"
else
    echo "Unable to calculate estimated size."
fi  

exit
Run Code Online (Sandbox Code Playgroud)

此外,这个答案也有很大帮助:Shell 命令对整数求和,每行一个?