Ted's Blog

Happy coding

Wget用法、参数解释的比较好的一个文章

Ted posted @ 2008年8月21日 00:17 in 未分类 with tags wget , 17608 阅读

wget是一个从网络上自动下载文件的自由工具。它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理.

所谓的自动下载是指,wget可以在用户退出系统的之后在后台执行。这意味这你可以登录系统,启动一个wget下载任务,然后退出系统,wget将在后台执行直到任务完成,相对于其它大部分浏览器在下载大量数据时需要用户一直的参与,这省去了极大的麻烦。
wget 可以跟踪HTML页面上的链接依次下载来创建远程服务器的本地版本,完全重建原始站点的目录结构。这又常被称作"递归下载"。在递归下载的时候,wget 遵循Robot Exclusion标准(/robots.txt). wget可以在下载的同时,将链接转换成指向本地文件,以方便离线浏览。
wget 非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性.如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕。如果是服务 器打断下载过程,它会再次联到服务器上从停止的地方继续下载。这对从那些限定了链接时间的服务器上下载大文件非常有用。
wget的常见用法
wget虽然功能强大,但是使用起来还是比较简单的,
基本的语法是:wget [参数列表] "URL" 用""引起来可以避免因URL中有特殊字符造成的下载出错。
下面就结合具体的例子来说明一下wget的用法。
    1、下载整个http或者ftp站点。
    wget http://place.your.url/here
    这个命令可以将http://place.your.url/here 首页下载下来。使用-x会强制建立服务器上一模一样的目录,如果使用-nd参数,那么服务器上下载的所有内容都会加到本地当前目录。

    wget -r http://place.your.url/here
    这个命令会按照递归的方法,下载服务器上所有的目录和文件,实质就是下载整个网站。这个命令一定要小心使用,因为在下载的时候,被下载网站指向的所有地址 同样会被下载,因此,如果这个网站引用了其他网站,那么被引用的网站也会被下载下来!基于这个原因,这个参数不常用。可以用-l number参数来指定下载的层次。例如只下载两层,那么使用-l 2。

    要是您想制作镜像站点,那么可以使用-m参数,例如:wget -m http://place.your.url/here
    这时wget会自动判断合适的参数来制作镜像站点。此时,wget会登录到服务器上,读入robots.txt并按robots.txt的规定来执行。

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

    3、批量下载。
    如果有多个文件需要下载,那么可以生成一个文件,把每个文件的URL写一行,例如生成文件download.txt,然后用命令:wget -i download.txt
这样就会把download.txt里面列出的每个URL都下载下来。(如果列的是文件就下载文件,如果列的是网站,那么下载首页)

    4、选择性的下载。
    可以指定让wget只下载一类文件,或者不下载什么文件。例如:
    wget -m --reject=gif http://target.web.site/subdirectory
    表示下载http://target.web.site/subdirectory,但是忽略gif文件。--accept=LIST 可以接受的文件类型,--reject=LIST拒绝接受的文件类型。

    5、密码和认证。
    wget只能处理利用用户名/密码方式限制访问的网站,可以利用两个参数:
    --http-user=USER设置HTTP用户
    --http-passwd=PASS设置HTTP密码
    对于需要证书做认证的网站,就只能利用其他下载工具了,例如curl。

    6、利用代理服务器进行下载。
    如果用户的网络需要经过代理服务器,那么可以让wget通过代理服务器进行文件的下载。此时需要在当前用户的目录下创建一个.wgetrc文件。文件中可以设置代理服务器:
    http-proxy = 111.111.111.111:8080
    ftp-proxy = 111.111.111.111:8080
    分别表示http的代理服务器和ftp的代理服务器。如果代理服务器需要密码则使用:
    --proxy-user=USER设置代理用户
    --proxy-passwd=PASS设置代理密码
    这两个参数。
    使用参数--proxy=on/off 使用或者关闭代理。
    wget还有很多有用的功能,需要用户去挖掘。




wget的使用格式
Usage: wget [OPTION]... [URL]...
* 用wget做站点镜像:
wget -r -p -np -k http://dsec.pku.edu.cn/~usr_name/
# 或者
wget -m http://dsec.pku.edu.cn/~usr_name/
* 在不稳定的网络上下载一个部分下载的文件,以及在空闲时段下载
wget -t 0 -w 31 -c http://dsec.pku.edu.cn/BBC.avi -o down.log &
# 或者从filelist读入要下载的文件列表
wget -t 0 -w 31 -c -B ftp://dsec.pku.edu.cn/linuxsoft -i filelist.txt -o down.log &
上面的代码还可以用来在网络比较空闲的时段进行下载。我的用法是:在mozilla中将不方便当时下载的URL链接拷贝到内存中然后粘贴到文件filelist.txt中,在晚上要出去系统前执行上面代码的第二条。
* 使用代理下载
wget -Y on -p -k https://sourceforge.net/projects/wvware/
代理可以在环境变量或wgetrc文件中设定
# 在环境变量中设定代理
export PROXY=http://211.90.168.94:8080/
# 在~/.wgetrc中设定代理
http_proxy = http://proxy.yoyodyne.com:18023/
ftp_proxy = http://proxy.yoyodyne.com:18023/


wget各种选项分类列表
* 启动
  -V,  --version           显示wget的版本后退出
  -h,  --help              打印语法帮助
  -b,  --background        启动后转入后台执行
  -e,  --execute=COMMAND   执行`.wgetrc'格式的命令,wgetrc格式参见/etc/wgetrc或~/.wgetrc
* 记录和输入文件
  -o,  --output-file=FILE     把记录写到FILE文件中
  -a,  --append-output=FILE   把记录追加到FILE文件中
  -d,  --debug                打印调试输出
  -q,  --quiet                安静模式(没有输出)
  -v,  --verbose              冗长模式(这是缺省设置)
  -nv, --non-verbose          关掉冗长模式,但不是安静模式
  -i,  --input-file=FILE      下载在FILE文件中出现的URLs
  -F,  --force-html           把输入文件当作HTML格式文件对待
  -B,  --base=URL             将URL作为在-F -i参数指定的文件中出现的相对链接的前缀
       --sslcertfile=FILE     可选客户端证书
       --sslcertkey=KEYFILE   可选客户端证书的KEYFILE
       --egd-file=FILE        指定EGD socket的文件名
* 下载
       --bind-address=ADDRESS   指定本地使用地址(主机名或IP,当本地有多个IP或名字时使用)
  -t,  --tries=NUMBER           设定最大尝试链接次数(0 表示无限制).
  -O   --output-document=FILE   把文档写到FILE文件中
  -nc, --no-clobber             不要覆盖存在的文件或使用.#前缀
  -c,  --continue               接着下载没下载完的文件
       --progress=TYPE          设定进程条标记
  -N,  --timestamping           不要重新下载文件除非比本地文件新
  -S,  --server-response        打印服务器的回应
       --spider                 不下载任何东西
  -T,  --timeout=SECONDS        设定响应超时的秒数
  -w,  --wait=SECONDS           两次尝试之间间隔SECONDS秒
       --waitretry=SECONDS      在重新链接之间等待1...SECONDS秒
       --random-wait            在下载之间等待0...2*WAIT秒
  -Y,  --proxy=on/off           打开或关闭代理
  -Q,  --quota=NUMBER           设置下载的容量限制
       --limit-rate=RATE        限定下载输率
* 目录
  -nd  --no-directories            不创建目录
  -x,  --force-directories         强制创建目录
  -nH, --no-host-directories       不创建主机目录
  -P,  --directory-prefix=PREFIX   将文件保存到目录 PREFIX/...
       --cut-dirs=NUMBER           忽略 NUMBER层远程目录
* HTTP 选项
       --http-user=USER      设定HTTP用户名为 USER.
       --http-passwd=PASS    设定http密码为 PASS.
  -C,  --cache=on/off        允许/不允许服务器端的数据缓存 (一般情况下允许).
  -E,  --html-extension      将所有text/html文档以.html扩展名保存
       --ignore-length       忽略 `Content-Length'头域
       --header=STRING       在headers中插入字符串 STRING
       --proxy-user=USER     设定代理的用户名为 USER
       --proxy-passwd=PASS   设定代理的密码为 PASS
       --referer=URL         在HTTP请求中包含 `Referer: URL'头
  -s,  --save-headers        保存HTTP头到文件
  -U,  --user-agent=AGENT    设定代理的名称为 AGENT而不是 Wget/VERSION.
       --no-http-keep-alive  关闭 HTTP活动链接 (永远链接).
       --cookies=off         不使用 cookies.
       --load-cookies=FILE   在开始会话前从文件 FILE中加载cookie
       --save-cookies=FILE   在会话结束后将 cookies保存到 FILE文件中
* FTP 选项
  -nr, --dont-remove-listing   不移走 `.listing'文件
  -g,  --glob=on/off           打开或关闭文件名的 globbing机制
       --passive-ftp           使用被动传输模式 (缺省值).
       --active-ftp            使用主动传输模式
       --retr-symlinks         在递归的时候,将链接指向文件(而不是目录)
* 递归下载
  -r,  --recursive          递归下载--慎用!
  -l,  --level=NUMBER       最大递归深度 (inf 或 0 代表无穷).
       --delete-after       在现在完毕后局部删除文件
  -k,  --convert-links      转换非相对链接为相对链接
  -K,  --backup-converted   在转换文件X之前,将之备份为 X.orig
  -m,  --mirror             等价于 -r -N -l inf -nr.
  -p,  --page-requisites    下载显示HTML文件的所有图片
* 递归下载中的包含和不包含(accept/reject)
  -A,  --accept=LIST                分号分隔的被接受扩展名的列表
  -R,  --reject=LIST                分号分隔的不被接受的扩展名的列表
  -D,  --domains=LIST               分号分隔的被接受域的列表
       --exclude-domains=LIST       分号分隔的不被接受的域的列表
       --follow-ftp                 跟踪HTML文档中的FTP链接
       --follow-tags=LIST           分号分隔的被跟踪的HTML标签的列表
  -G,  --ignore-tags=LIST           分号分隔的被忽略的HTML标签的列表
  -H,  --span-hosts                 当递归时转到外部主机
  -L,  --relative                   仅仅跟踪相对链接
  -I,  --include-directories=LIST   允许目录的列表
  -X,  --exclude-directories=LIST   不被包含目录的列表
  -np, --no-parent                  不要追溯到父目录
问题
在递归下载的时候,遇到目录中有中文的时候,wget创建的本地目录名会用URL编码规则处理。如"天网防火墙"会被存为"%CC%EC%CD%F8%B7%C0%BB%F0%C7%BD",这造成阅读上的
Avatar_small
Arianna 说:
2020年10月24日 18:04

I am genuinely thankful to the holder of this web page who has shared this wonderful paragraph at at this place  vsop price

Avatar_small
Arianna 说:
2020年10月26日 02:35

I must admit that your post is really interesting. I have spent a lot of my spare time reading your content. Thank you a lot!  Hacer ejercicio en casa

Avatar_small
Arianna 说:
2020年10月27日 02:09

Always so interesting to visit your site.What a great info, thank you for sharing. this will help me so much in my learning   Funny t-shirts

Avatar_small
Arianna 说:
2020年10月27日 21:19

A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.   boring blues guitar solos

Avatar_small
Arianna 说:
2020年10月29日 01:48

I want to say thanks to you. I have bookmark your site for future updates.  Buy Green Xanax Online

Avatar_small
Arianna 说:
2020年10月31日 01:30

Always so interesting to visit your site.What a great info, thank you for sharing. this will help me so much in my learning  hairpexin

Avatar_small
Arianna 说:
2020年11月01日 01:53

I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!   Barrington HVAC

Avatar_small
Arianna 说:
2020年11月01日 23:16

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.  instagram panel

Avatar_small
Arianna 说:
2020年11月02日 20:40

Really appreciate this wonderful post that you have provided for us.Great site and a great topic as well i really get amazed to read this. Its really good.  wedding dress rental

Avatar_small
Arianna 说:
2020年11月04日 00:44

It's really nice and meanful. it's really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information.   bto renovation packages

Avatar_small
Arianna 说:
2020年11月04日 23:34

Its as if you had a great grasp on the subject matter, but you forgot to include your readers. Perhaps you should think about this from more than one angle.  website design company

Avatar_small
Arianna 说:
2020年11月07日 01:32

This website is remarkable information and facts it's really excellent   photo lab pro online apk

Avatar_small
Arianna 说:
2020年11月08日 17:22

I really loved reading your blog. It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot!    best place to buy semi truck parts online

Avatar_small
Arianna 说:
2020年11月09日 19:35

This is a splendid website! I"m extremely content with the remarks!.  Facebook panel

Avatar_small
Arianna 说:
2020年11月15日 20:45

Always so interesting to visit your site.What a great info, thank you for sharing. this will help me so much in my learning  buy percodan online

Avatar_small
Arianna 说:
2020年11月17日 19:52

 With so many books and articles coming up to give gateway to make-money-online field and confusing reader even more on the actual way of earning money,  CAr T-Shirts

Avatar_small
Arianna 说:
2020年11月18日 18:28

This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post.  apps download for pc

Avatar_small
Arianna 说:
2020年11月20日 02:19

Great Information sharing .. I am very happy to read this article .. thanks for giving us go through info.Fantastic nice. I appreciate this post.  auto insurance

Avatar_small
Arianna 说:
2020年11月21日 16:37

Welcome to the party of my life here you will learn everything about me.  Best VR Headset

Avatar_small
Arianna 说:
2020年11月23日 01:23

A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one  Furniture Removals

Avatar_small
Arianna 说:
2020年11月24日 01:59

This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog..  espiar movil gratis

Avatar_small
Arianna 说:
2020年11月25日 00:05

I think about it is most required for making more on this get engaged  mastering google ads course review

Avatar_small
Arianna 说:
2020年11月28日 14:46

I think about it is most required for making more on this get engaged  Mini cabra a venda

Avatar_small
UMAIR 说:
2021年2月20日 15:09

AP Board 10th Model Paper 2022 AP Board 10th Model Paper 2022 SEBA 10th Model Paper 2022 Assam HSLC Question Paper 2022 Bihar 10th Model Paper 2022 BSEB Matric Blueprint 2022 Question Paper 2022 CG Board 10th Model Paper 2022 CGBSE 12th Question Paper 2079

Avatar_small
UMAIR 说:
2021年3月02日 16:34

If you want to convert your PDF documents by maintaining the quality then you should visit https://altoconvertpdftopng.com/blog blog. They provide the best services which are fast and of high quality.

Avatar_small
UMAIR 说:
2021年3月10日 19:23

Really impressed! Everything is very open and very clear clarification of issues. It contains truly facts. Your website is very valuable. Thanks for sharing. It's late finding this act. PAKSEO.NET PROVIDES QUALITY SEO SERVICES At least, it's a thing to be familiar with that there are such events exist. I agree with your Blog and I will be back to inspect it more in the future so please keep up your act.

Avatar_small
dark web/deep web/d 说:
2022年8月04日 18:54

Many web sites and pages are made with JavaScript code that is hidden. This means that your computer can only see a part of the web sites that you are trying to access.   dark web links

Avatar_small
dark web/deep web/d 说:
2022年8月04日 19:39

By having an advertising campaign on your website, you can expose yourself and your company to serious financial danger.  deep web

Avatar_small
dark web/deep web/d 说:
2022年8月04日 19:53

Just because you don't have to take the risk of exposing your identity to strangers on the Internet doesn't mean that you shouldn't use these valuable resources responsibly. dark web links

Avatar_small
dark web/deep web/d 说:
2022年8月04日 20:15

For instance, I have friends who are avidly participating in chat rooms as well. It seems that there is no end to the dark web activities that people are engaging in.  dark web sites

Avatar_small
dark web/deep web/d 说:
2022年8月04日 20:31

It's possible to make a lot of money with bitcoins, especially if you combine this opportunity with a bit of skill. The more you know about the dark web, the more profitable it will be for you.   dark web

Avatar_small
dark web/deep web/d 说:
2022年8月04日 20:47

Chia does point out some good pointers as to why some niches are much easier to promote than others, and you will want to make sure that you take advantage of those opportunities.  work from home jobs

Avatar_small
dark web/deep web/d 说:
2022年8月04日 21:03

It wasn't really the product itself; instead, it was their willingness to partner with others in the weight-loss niche that made the partnership so valuable.  affiliate marketing success

Avatar_small
SHIA YASSARNAL QURA 说:
2023年9月08日 05:25

Our Online Shia Yassarnal Quran program has created a golden opportunity for you in this regard. It wants to cater to you with the best online courses.

This whole thing is a different process and course that you learn by reading Shia Yassarnal Quran.

We offer the courses like Shia Online Yassarnal Quran. It is not an ordinary course but is of much importance.

Yassarnal Quran Qaida Online Has created so much ease for Muslims. When you start anything with full passion, preparation and motivation then there are a larger number of chances that you will be able to pull that thing in the most excellent way in the longer run.

Shia Quran Qaida Online gave us a new vision and idea with a proper methodology. In order to treat these students in a certain way. After launching this program on public demand we launched a lot of other programs.

 

Avatar_small
jsimitseo 说:
2024年1月30日 21:50

I urge you to peruse this content it is fun portrayed ...  best concierge doctor


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter