如果仅是简单的HTTP(S)/FTP下载,Wget显然更加合适,对于复杂的工作则应该尝试使用cURL。

cURL

  • cURL—-Command Line URL Viewer,包含curl与libcurl,支持丰富的通信协议,可自定义headers,请求方式等,常用于网页调试。
  • Wget—-World Wide Web Get,仅为命令行工具,无lib,使用简单。仅支持HTTPS HTTP与FTP,支持递归,使用-c参数可用于断点续传。

在终端中输入curl —help即可获取到使用帮助,下面挑选一些有意思的参数(注意大小写):

  • -b, —cookie Send cookies from string/file

    用来发送Cookie参数,例如curl -b"login=tru;expire=false" https://google.com,将login=true,expire=false的Cookie发送。

  • -A, —user-agent Send User-Agent to server

    设置HTTP请求的User-Agent值,例:
    curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" https://google.com

  • -c, —cookie-jar Write cookies to after operation

    请求完成后将服务器设置的Cookie写入filename,例curl -c cookies.txt https://google.com

  • -d, —data HTTP POST data

    将data使用POST发送到目标网站,例:
    curl -d "username=raincorn" -d "password=qwq" https://google.com,注意此种方式默认使用POST提交数据,当然也可以添加-G参数,使用GET提交数据。

  • -F, —form Specify multipart MIME data

    用于向服务器上传二进制文件,例:
    curl -F "[email protected];type=image/png" https://google.com,type参数用于指定MIME类型,若未指定则默认为application/octet-stream。

  • -G, —get Put the post data in the URL and use GET

    GET方式上传data中的数据,见前文-d参数。

  • -i, —include Include protocol response headers in the output

    打印http response的头信息并空行打印网页HTML源代码,例curl -i https://google.cn,返回如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
HTTP/2 302 
location: http://www.google.cn/
cache-control: private
content-type: text/html; charset=UTF-8
p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
date: Wed, 29 Apr 2020 14:05:18 GMT
server: gws
content-length: 218
x-xss-protection: 0
x-frame-options: SAMEORIGIN
set-cookie: 1P_JAR=2020-04-29-14; expires=Fri, 29-May-2020 14:05:18 GMT; path=/; domain=.google.cn; Secure
set-cookie: NID=203=moQbiUF64KWS7sOEDAqvfqxaoouJf0FR56EjYsAEb8qxx_S0nBbhaRvpe3d-_0RxU9mhN4e2-roIj20g7H5tngTmzTCqVf5fRRB3vnGywZzfCf0uaF5YwYqKkf0qVtJahbg7d8NOimD5uCMCSVMawNe7Ej3CZCE6KPM3Tj_VH2Q; expires=Thu, 29-Oct-2020 14:05:18 GMT; path=/; domain=.google.cn; HttpOnly
alt-svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,h3-T050=":443"; ma=2592000

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.cn/">here</A>.
</BODY></HTML>
  • -I, —head Show document info only

    仅打印http response的头信息,而不带源码。

  • -H, —header header/@file Pass custom header(s) to server

    指定HTTP请求的header部分,例curl -H "Connection: keep-alive" https://google.com

  • -m, —max-time Maximum time allowed for the transfer

    指定网页的超时时间。

  • -e, —referer Referrer URL

    设置HTTP请求头的Referrer部分,用于向网站表明流量来源,常用在有防盗链处理的网站上,例curl -e "https://google.cn" https://google.com

  • -X, —request Specify request command to use

    设置HTTP请求方式,包括GET POST PUT DELETE等。

  • -v, —verbose Make the operation more talkative

    将整个网络请求更加可视化,显示整个过程,例curl -v https://google.com。也可以使用—trace参数,将会输出原始二进制数据。

  • -x, —proxy [protocol://]host[:port] Use this proxy

    通过本地/远程代理来请求网页数据。

  • —resolve Resolve the host+port to this address

    强制将host:port解析到address。

  • —data-urlencode HTTP POST data url encoded

    将URL进行编码,当URL中含有空格或者其他情况时使用。

参考链接:

curl的用法指南

curl vs Wget