我使用wget下载网站内容,但是wget是一个一个下载文件的。
我怎么能让wget下载使用4个同时连接?
我使用wget下载网站内容,但是wget是一个一个下载文件的。
我怎么能让wget下载使用4个同时连接?
当前回答
由于还没有提到GNU并行,让我给出另一种方式:
cat url.list | parallel -j 8 wget -O {#}.html {}
其他回答
make可以很容易地并行化(例如,make -j 4)。例如,这是一个简单的Makefile,我正在使用wget并行下载文件:
BASE=http://www.somewhere.com/path/to
FILES=$(shell awk '{printf "%s.ext\n", $$1}' filelist.txt)
LOG=download.log
all: $(FILES)
echo $(FILES)
%.ext:
wget -N -a $(LOG) $(BASE)/$@
.PHONY: all
default: all
正如其他海报所提到的,我建议你看一看aria2。Ubuntu 1.16.1版本的手册页:
aria2 is a utility for downloading files. The supported protocols are HTTP(S), FTP, BitTorrent, and Metalink. aria2 can download a file from multiple sources/protocols and tries to utilize your maximum download bandwidth. It supports downloading a file from HTTP(S)/FTP and BitTorrent at the same time, while the data downloaded from HTTP(S)/FTP is uploaded to the BitTorrent swarm. Using Metalink's chunk checksums, aria2 automatically validates chunks of data while downloading a file like BitTorrent.
您可以使用-x标志来指定每个服务器的最大连接数(默认为1):
aria2c -x 16 [url]
如果同一文件可从多个位置下载,则可以选择从所有位置下载。使用-j标志指定每个静态URI的最大并行下载数量(默认为5)。
aria2c -j 5 [url] [url2]
更多信息请访问http://aria2.sourceforge.net/。对于使用信息,手册页是真正的描述性的,并在底部有一个小节提供了使用示例。在线版本可以在http://aria2.sourceforge.net/manual/en/html/README.html上找到。
为每个链接调用Wget并将其设置为在后台运行。
我尝试了这段Python代码
with open('links.txt', 'r')as f1: # Opens links.txt file with read mode
list_1 = f1.read().splitlines() # Get every line in links.txt
for i in list_1: # Iteration over each link
!wget "$i" -bq # Call wget with background mode
参数:
b - Run in Background
q - Quiet mode (No Output)
如果你在做递归下载,你还不知道所有的url, wget是完美的。
如果您已经有了想要下载的每个URL的列表,那么可以跳过下面的cURL。
使用Wget递归地进行多个同时下载(未知的url列表)
# Multiple simultaneous donwloads
URL=ftp://ftp.example.com
for i in {1..10}; do
wget --no-clobber --recursive "${URL}" &
done
上面的循环将启动10个wget,每个wget都递归地从同一个网站下载,但是它们不会重叠或下载同一文件两次。
使用——no-clobber可以防止10个wget进程中的每个进程两次下载同一个文件(包括完整的相对URL路径)。
& fork每个wget到后台,允许你运行多个同时下载从同一个网站使用wget。
从url列表中使用curl
如果你已经有了一个想要下载的url列表,curl -Z是并行的curl,默认一次运行50个下载。
然而,对于curl,列表必须是这样的格式:
url = https://example.com/1.html
-O
url = https://example.com/2.html
-O
因此,如果您已经有一个要下载的url列表,只需格式化该列表,然后运行cURL
cat url_list.txt
#https://example.com/1.html
#https://example.com/2.html
touch url_list_formatted.txt
while read -r URL; do
echo "url = ${URL}" >> url_list_formatted.txt
echo "-O" >> url_list_formatted.txt
done < url_list.txt
使用curl从url列表中并行下载:
curl -Z --parallel-max 100 -K url_list_formatted.txt
例如,
$ curl -Z --parallel-max 100 -K url_list_formatted.txt
DL% UL% Dled Uled Xfers Live Qd Total Current Left Speed
100 -- 2512 0 2 0 0 0:00:01 0:00:01 --:--:-- 1973
$ ls
1.html 2.html url_list_formatted.txt url_list.txt
一个新的(但尚未发布的)工具是Mget。 它已经从Wget中获得了许多选项,并提供了一个库,允许您轻松地将(递归)下载嵌入到您自己的应用程序中。
回答你的问题:
Mget——num-threads=4 [url]
更新
Mget现在开发为Wget2,修复了许多错误,增加了更多的功能(例如HTTP/2支持)。
——num-threads现在是——max-threads。