访问的链接来源做了访问限制,这些限制几乎都是通过referer来实现的 。
比如:要求是先访问首页,然后再访问首页中的邮箱页面,这时访问邮箱的referer地址就是访问首页成功后的页面地址 。如果服务器发现对邮箱页面访问的referer地址不是首页的地址,就断定那是个盗连了 。
可以通过 -e, --referer 或则 -H, --header <header> 实现伪造 referer。
curl -e 'https://www.baidu.com' http://www.zhangblog.com/2019/06/24/domainexpire/或者curl -H 'Referer: https://www.baidu.com' http://www.zhangblog.com/2019/06/24/domainexpire/构造HTTP请求头
可以通过 -H, --header <header> 实现构造http请求头 。
curl -H 'Connection: keep-alive' -H 'Referer: https://sina.com.cn' -H 'User-Agent: Mozilla/1.0' http://www.zhangblog.com/2019/06/24/domainexpire/
保存响应头信息
可以通过 -D, --dump-header <file> 选项实现 。
[root@iZ28xbsfvc4Z 20190703]# curl -D baidu_header.info www.baidu.com ………………[root@iZ28xbsfvc4Z 20190703]# lltotal 4-rw-r--r-- 1 root root 400 Jul 3 10:11 baidu_header.info # 生成的头文件限时访问
--connect-timeout <seconds> 连接服务端的超时时间 。这只限制了连接阶段,一旦curl连接了此选项就不再使用了 。
# 当前 https://www.zhangXX.com 是国外服务器,访问受限[root@iZ28xbsfvc4Z ~]# curl --connect-timeout 10 https://www.zhangXX.com | head% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed0 0 0 0 0 0 0 0 --:--:-- 0:00:10 --:--:-- 0curl: (28) Connection timed out after 10001 milliseconds-m, --max-time <seconds> 允许整个操作花费的最大时间(以秒为单位) 。这对于防止由于网络或链接变慢而导致批处理作业挂起数小时非常有用 。
[root@iZ28xbsfvc4Z ~]# curl -m 10 --limit-rate 5 http://www.baidu.com/ | head # 超过10秒后,断开连接% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed2 2381 2 50 0 0 4 0 0:09:55 0:00:10 0:09:45 4curl: (28) Operation timed out after 10103 milliseconds with 50 out of 2381 bytes received<!DOCTYPE html><!--STATUS OK--><html> <head><met### 或[root@iZ28xbsfvc4Z ~]# curl -m 10 https://www.zhangXX.com | head # 超过10秒后,断开连接% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed0 0 0 0 0 0 0 0 --:--:-- 0:00:10 --:--:-- 0curl: (28) Connection timed out after 10001 milliseconds显示抓取错误
当我们请求访问失败时或者没有该网页时,网站一般都会给出一个错误的提示页面 。
如果我们不需要这个错误页面,只想得到简洁的错误信息 。那么可以通过 -f, --fail 选项实现 。
[root@iZ28xbsfvc4Z 20190713]# curl http://www.zhangblog.com/201912312<html><head><title>404 Not Found</title></head><body bgcolor="white"><center><h1>404 Not Found</h1></center><hr><center>nginx/1.14.2</center></body></html>[root@iZ28xbsfvc4Z 20190713]# curl -f http://www.zhangblog.com/201912312 # 得到更简洁的错误信息curl: (22) The requested URL returned error: 404 Not Found表单登录与cookie使用
参见「Linux curl 表单登录或提交与cookie使用」:http://www.zhangblog.com/2019/07/20/curl03/
2.文件上传与下载涉及 FTP 服务,简单快速搭建可参考:《centos7下安装FTP服务》「https://www.cnblogs.com/zhi-leaf/p/5983550.html」
文件下载网页文件下载
# 以进度条展示,而不是进度表展示[root@iZ28xbsfvc4Z 20190715]# curl -# -o tmp.data2 http://www.zhangblog.com/uploads/tmp/tmp.data######################################################################## 100.0%FTP文件下载
说明1:其中 ftp1 用户是ftp服务端的账号,具体家目录是:/mnt/ftp1
说明2:当我们使用 curl 通过 FTP 进行下载时,后面跟的路径都是:当前使用的 ftp 账号家目录为基础的相对路径,然后找到的目标文件 。
示例1
# 其中 tmp.data 的绝对路径是:/mnt/ftp1/tmpdata/tmp.data ;ftp1 账号的家目录是:/mnt/ftp1# 说明:/tmpdata/tmp.data 这个路径是针对 ftp1 账号的家目录而言的[yun@nginx_proxy01 20190715]$ curl -O ftp://ftp1:123456@172.16.1.195:21/tmpdata/tmp.data# 或者[yun@nginx_proxy01 20190715]$ curl -O -u ftp1:123456 ftp://172.16.1.195:21/tmpdata/tmp.data% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed100 2048M 100 2048M 0 0 39.5M 0 0:00:51 0:00:51 --:--:-- 143M
推荐阅读
- 淘宝一年以上的订单还可以评价吗 淘宝客低价冲销量后涨价
- 淘宝直播的红包怎么用 点淘直播间红包雨怎么抢
- 基于分布式系统的7种唯一ID实现方案
- Excel批量自动发送带附件的邮件
- 服务器被黑了怎么办?
- 紫阳富硒茶减肥的效果
- 喝紫阳富硒茶减肥的注意事项
- .NET导出Excel的四种方法及评测
- 那些只有玩光遇才知道的梗 光遇怎么加好友
- 一文搞懂跨域的所有问题