我使用wget下载网站内容,但是wget是一个一个下载文件的。

我怎么能让wget下载使用4个同时连接?


当前回答

使用咏叹调2:

aria2c -x 16 [url]
#          |
#          |
#          |
#          ----> the number of connections 

http://aria2.sourceforge.net

其他回答

为了加快文件下载速度,Wget不支持多个套接字连接。

我想我们可以给出比格里安更好的答案。

正确的方法是使用aria2。

aria2c -x 16 -s 16 [url]
#          |    |
#          |    |
#          |    |
#          ---------> the number of connections here

官方文档:

-x,——max-connection-per-server=NUM:每次下载到一台服务器的最大连接数。取值范围:1 ~ 16。默认值:1

-s,——split=N:使用N个连接下载文件。如果提供了超过N个uri,则使用前N个uri,其余url用于备份。如果给出的uri少于N个,则这些url将被使用不止一次,以便同时建立总共N个连接。到同一主机的连接数量由——max-connection-per-server选项限制。另参见——min-split-size选项。取值范围:1-*默认值:5

为每个链接调用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

use

aria2c -x 10 -i websites.txt >/dev/null 2>/dev/null &

在sites.txt中每行放一个url,例如:

https://www.example.com/1.mp4
https://www.example.com/2.mp4
https://www.example.com/3.mp4
https://www.example.com/4.mp4
https://www.example.com/5.mp4

使用xargs使wget在多个文件中并行工作

#!/bin/bash

mywget()
{
    wget "$1"
}

export -f mywget

# run wget in parallel using 8 thread/connection
xargs -P 8 -n 1 -I {} bash -c "mywget '{}'" < list_urls.txt

Aria2选项,正确的工作方式与文件小于20mb

aria2c -k 2M -x 10 -s 10 [url]

-k 2M将文件分割成2mb的块

-k或——min-split-size的默认值是20mb,如果你不设置这个选项并且文件小于20mb,无论-x或-s的值是多少,它都只会在单个连接中运行