# 基本使用
# 1、基本用法
curl http://www.baidu.com
显示出网页的 html
# 2、保存访问的网页
# 2.1 使用 linux 的重定向功能保存
curl URL >> filename.html
curl http://www.baidu.com >> baidu.html
# 2.2 可以使用 curl 的内置选项 -o (小写) 保存网页
curl -o filename.html URL
curl -o baidu.html http://www.baidu.com
# 2.3 可以使用 curl 的内置选项 -O (大写) 保存网页中的文件
curl -O filename.html URL / 文件名。后缀名
curl -O http://www.baidu.com/hello.sh
# 3、测试网页返回值 (响应码)
curl -o /dev/null -s -w %{http_code} URL
curl -o /dev/null -s -w %{http_code} www.baidu.com
# 4、指定 proxy 服务器以及其端口
curl -x 192.168.100.100:1080 http://www.baidu.com
# 5、COOKIES
注意❗:-c (小写) 产生的 cookie 和 -D 里面的 cookie 是不一样的。
# 5.1 保存 http 的 response 里面的 cookie 信息。内置选项 -c(小写)
curl -c cookie.txt http://www.linux.com (执行后 cookie 会被保存到 cookie.txt 中)
# 5.2 保存 http 的 response 里面的 header 信息。内置 option: -D
curl -D cookied.txt http://www.baidu.com
# 5.3 使用 cookie
curl -b cookiec.txt http://www.linux.com
curl -b cookies.txt -c newcookies.txt www.cookiesite. com 、
# 6、模仿浏览器
curl -A “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)” http://www.linux.com
# 7、伪造 referer(盗链)
很多服务器会检查 http 访问的 referer 从而来控制访问。比如:你是先访问首页,然后再访问首页中的邮箱页面,
这里访问邮箱的 referer 地址就是访问首页成功后的页面地址,如果服务器发现对邮箱页面访问的 referer 地址
不是首页的地址,就断定那是个盗连了。
curl -e “www.linux.com” http://mail.linux.com
# 8、curl 下载文件
# 8.1 利用 curl 下载文件
curl -o (小写)图片名称.jpg URL # 图片地址
curl -o bmw.jpg https://scpic.chinaz.net/files/pic/pic9/202205/apic40779.jpg
curl -O (大写)URL
curl -O https://scpic.chinaz.net/files/pic/pic9/202205/apic40779.jpg
# 8.2 循环下载 (下载具有相同字段的文件)
curl -O http://www.linux.com/dodo[1-5].JPG
# 8.3 下载重命名
curl -o #1_#2.JPG http://www.linux.com/{hello,bb}/dodo[1-5].JPG (不明白)