玩儿虫那些事(四)—— 使用curl

目录

  • 一、爬一个简单的网站
  • 二、模拟登录新浪
  • 三、各种请求的发送
  • 四、使用curl
  • 五、模拟登录QQ空间
  • 六、selenium的使用
  • 七、phantomjs的使用
  • 八、开源框架webmagic
  • 九、开源框架scrapy
  • 十、多线程爬取与反爬策略
  • 十一、加密与解密
  • 十二、APP数据抓取
  • 十三、分布式爬虫
1、 发送一个简单的请求,获取页面,并查看请求头
curl https://www.baidu.com

curl https://www.baidu.com --head
2、 下载某一资源
curl https://www.baidu.com/1.png
3、将请求到的文件输出到某个文件中
curl https://www.baidu.com -o baidu.txt
4、 请求需要验证 HTTP authentication
curl -u username:password https://xxxx.com
5、 在配置中进行批量操作
curl -K cmdline.txt https://baidu.com

cmdline.txt

--location

--head

(6) 分析请求和响应,并将响应结果保存


curl -v https://baidu.com -o response.txt

执行结果如下


* Rebuilt URL to: http://www.baidu.com/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 119.75.213.61...
* TCP_NODELAY set
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to www.baidu.com (119.75.213.61) port 80 (#0)
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: bfe/1.0.8.18
< Date: Thu, 18 Jan 2018 17:17:36 GMT
< Content-Type: text/html
< Content-Length: 2381
< Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
< Connection: Keep-Alive
< ETag: "588604c8-94d"
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Pragma: no-cache
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< Accept-Ranges: bytes
<
{ [2381 bytes data]
100  2381  100  2381    0     0   3770      0 --:--:-- --:--:-- --:--:--  3767
* Connection #0 to host www.baidu.com left intact

7、使用post发送数据

curl -d 'name=amdini&passwd=123' http://example.com

curl -d '{json}'  -H  'Content-Type:application/json' http://example.com

8、对发送数据进行编码

curl --data-urlencode "name=John Doe(Junior)" http://example.com

请求的效果

name=John%20Doe%20%28Junior%29

9、发送multipart类型的数据

请求前的格式可能是这样的

<form action="submit.cgi" method="post" enctype="multipart/form-data">
Name: <input type="text" name="person"><br>
File: <input type="file" name="secret"><br>
<input type="submit" value="Submit">
</form>

请求方法

可以使用-F--form

  curl -F person=anonymous -F secret=@file.txt http://example.com/submit.cgi

请求的效果

--------------------------d74496d66958873e
Content-Disposition: form-data; name="person"
anonymous
--------------------------d74496d66958873e
Content-Disposition: form-data; name="secret"; filename="file.txt"
Content-Type: text/plain
contents of the file
--------------------------d74496d66958873e

转载于:https://www.cnblogs.com/fonxian/p/8314527.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值