Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wget #60

Open
Qingquan-Li opened this issue Sep 25, 2017 · 0 comments
Open

Wget #60

Qingquan-Li opened this issue Sep 25, 2017 · 0 comments
Labels

Comments

@Qingquan-Li
Copy link
Owner

Qingquan-Li commented Sep 25, 2017

Wget ( “World Wide Web” 和 “Get” 的结合)是一个在网络上进行下载的简单而强大的自由软件。目前它支持通过 HTTP、HTTPS 、FTP 这三个最常见的 TCP/IP 协议协议下载。
1996年,随着互联网的爆发, Wget 出现了。由于使用移植性非常良好的 C 语言,所以 Wget 可以轻松的在任何类似 Unix 的系统以及其他 Unix 变种操作系统上编译使用。

macOS 安装 Wget : brew install wget
基本语法: wget [参数列表] URL
查看用法: wget -hwget --help
基础用法:

一、下载整个 http 或者 ftp 站点

示例:

  • wget http://example.com/about.html 下载 about.html 页面到本地( http 可省略)
  • wget example.com 下载该网站首页到本地
  • wget -r example.com 按照递归的方法,下载服务器上所有的目录和文件,实质就是下载整个网站到本地(在下载的时候,被下载网站指向的所有地址同 样会被下载,因此,如果这个网站引用了其他网站,那么被引用的网站也会被下载下来!基于这个原因,这个命令不常用)。
  • wget -m example.com 制作镜像站点( wget 会自动判断合适的参数来制作镜像站点。此时, wget 会登录到服务器上,读入 robots.txt 并按 robots.txt 的规定来执行)。

实例:下载一个技术文档类的网站。(注意,根据网站部署所在地,选择是否需要在命令行终端使用代理。)

$ wget -m -E -p -k -P /Users/fatli/python https://docs.gitbook.com

参考:wget 命令-中文手册
-m , --mirror -N -r -l inf --no-remove-listing 的缩写形式。
-E , --adjust-extension 以合适的扩展名保存 HTML/CSS 文档。
-p , --page-requisites 下载所有用于显示 HTML 页面的图片之类的元素。
-k , --convert-links 让下载得到的 HTML 或 CSS 中的链接指向本地文件。
-P , --directory-prefix=PREFIX 以 PREFIX/... 保存文件。


二、断点续传

  • 场景:当文件特别大或者网络特别慢的时候,往往一个文件还没有下载完,连接就已经被切断,此时就需要断点续传。
  • 方式:wget 的断点续传是自动的,只需要使用 -c 参数
    示例: wget -c http://example.com/file
  • 注意:使用断点续传要求服务器支持断点续传。 -t 参数表示重试次数,例如需要重试 100 次,那么就写 -t 100 ,如果设成 -t 0 ,那么表示无穷次重试,直到连接成功。 -T 参数表示超时等待时间,例如 -T 120 ,表示等待 120 秒连接不上就算超时。

三、批量下载

  • 场景:需要下载多个文件(文件或网站页面)
  • 用法:新建一个文件,把需要下载多个文件各写成一行,例如在 download 文件夹中新建一个 download.txt 文件,写入多行 URL ,使用 wget -i download.txt 进行批量下载。
  • :使用 wget -Q 限制总下载文件大小
    示例:wget -Q5m -i download.txt (下载的文件超过 5m 则退出下载)
    注意:这个参数对单个文件下载不起作用,只能递归下载时才有效。

四、选择性下载

  • 方式–accept=LIST 可以接受的文件类型, –reject=LIST 拒绝接受的文件类型。
    示例: wget -m –reject=gif http://example.com/image 表示下载 http://example.com/image ,但是忽略 gif 文件。

五、密码和认证

  • 场景wget 只能处理利用 用户名/密码 方式限制访问的网站,对于需要证书做认证的网站,就只能利用其他下载工具了,例如 curl
  • 用法:可以利用两个参数:
    —http-user=USER set http user to USER
    —http-password=PASS set http password to PASS
    示例(下载需要登录后才能访问的页面):
    wget --http-user=15626236918 --http-password=123456 http://example.com/index.html

了解更多:

@Qingquan-Li Qingquan-Li changed the title Homebrew:macOS软件包管理工具 。。。 Sep 25, 2017
@Qingquan-Li Qingquan-Li changed the title 。。。 Wget Oct 4, 2017
@Qingquan-Li Qingquan-Li changed the title Wget Wget/cURL Feb 5, 2018
@Qingquan-Li Qingquan-Li changed the title Wget/cURL Wget May 5, 2018
@Qingquan-Li Qingquan-Li added Linux and removed Others labels Nov 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant