cURL
1 用途
cURL是一个命令行方式下传输数据的开源传输工具。常被用来抓取网页和监控Web服务器状态。
2 支持协议
DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET 和 TFTP。
3 描述
The command is designed to work without user interaction.
The URL syntax is protocol-dependent. You'll find a detailed description in RFC 3986.
curl normally displays a progress meter during operations, indicating the amount of transferred data, transfer speeds and estimated time left, etc.
curl displays this data to the terminal by default, so if you invoke curl to do an operation and it is about to write data to the terminal, it disables the progress meter as otherwise it would mess up the output mixing progress meter and response data.
4 用法
1. linux curl抓取网页:
抓取百度:
curl http : //www.baidu.com
如发现乱码,可以使用iconv 转码:
curl http : //iframe.ip138.com/ic.asp|iconv -fgb2312
iconv的用法请参阅:在Linux/Unix系统下用iconv命令处理文本文件中文乱码问题
2. Linux curl使用代理 :
linux curl使用http代理 抓取页面:
curl - x 111.95.243.36 : 80 http : //iframe.ip138.com/ic.asp|iconv -fgb2312
curl - x 111.95.243.36 : 80 - U aiezu : password http : //www.baidu.com
使用socks代理抓取页面:
curl -- socks4 202.113.65.229 : 443 http : //iframe.ip138.com/ic.asp|iconv -fgb2312
curl -- socks5 202.113.65.229 : 443 http : //iframe.ip138.com/ic.asp|iconv -fgb2312
代理服务器地址可以从爬虫代理 上获取。
3. linux curl处理cookies
接收cookies :
curl - c / tmp / cookies http : //www.baidu.com #cookies保存到/tmp/cookies文件
发送cookies:
curl - b "key1=val1;key2=val2;" http : //www.baidu.com #发送cookies文本
curl - b / tmp / cookies http : //www.baidu.com #从文件中读取cookies
4. linux curl发送数据:
linux curl get 方式提交数据:
curl - G - d "name=value&name2=value2" http : //www.baidu.com
linux curl post 方式提交数据:
curl - d "name=value&name2=value2" http : //www.baidu.com #post数据
curl - d a = b & c = d & txt @ / tmp / txt http : //www.baidu.com #post文件
以表单的方式上传文件:
curl - F file = @ / tmp / me . txt http : //www.aiezu.com
相当于设置form表单的method="POST"和enctype='multipart/form-data'两个属性。
5. linux curl http header处理:
设置http请求头信息:
curl - A "Mozilla/5.0 Firefox/21.0" http : //www.baidu.com #设置http请求头User-Agent
curl - e "http://pachong.org/" http : //www.baidu.com #设置http请求头Referer
curl - H "Connection:keep-alive \n User-Agent: Mozilla/5.0" http : //www.aiezu.com
设置http响应头处理:
curl - I http : //www.aiezu.com #仅仅返回header
curl - D / tmp / header http : //www.aiezu.com #将http header保存到/tmp/header文件
6. linux curl认证:
curl - u aiezu : password http : //www.aiezu.com #用户名密码认证
curl - E mycert . pem https : //www.baidu.com #采用证书认证
7. 其他:
curl - # http://www.baidu.com #以“#”号输出进度条
curl - o / tmp / aiezu http : //www.baidu.com #保存http响应到/tmp/aiezu