python学习之curl使用

我爱海鲸 2024-10-09 16:58:03 python

简介curl爬虫的相关使用

1、安装linux虚拟机,在linux上输入curl,如图:

undefined

2、输入curl --version,如图:

undefined

就表示curl安装没有问题

3、如果没有按转curl  直接使用 apt install curl 安装即可,如出现安装报错的问题输入命令:apt install openssl、apt install openssl-dev安装依赖库

4、安装完以后,我们来是一个简单的curl命令,curl http://www.baidu.com如图:

undefined

上图中我们就看到了百度的源码,你可将curl认为是一个命令行的浏览器

5、一些常用的参数命令如下:

-A      设置user-agent                                         curl -A "Chrome" http://www.baidu.com

-X     用指定方法请求                                         curl -X POST http://httpbin.org/post

-I      只返回请求的头信息

-d     以POST方法请求url,并发送相应的参数 -d a=1 -d b=2 -d c=3 -d "a=1&b=2&c=3" -d @filename

-O   下载文件并以远程的文件名保存

-o    下载文件并以指定的文件名保存                 curl -o fox.jpeg http://httpbin.org/image/jpeg

-L    跟随重定向请求                                          curl -IL https://baidu.com

-H   设置头信息                                                 curl -o image.webp -H "accept:image/webp" http://httpbin.org/image

-k    允许发起不安全的SSL请求

-b   设置                                                            cookies curl -b a=test http://httpbin.org/cookies

-s   不显示其他无关信息

-v   显示连接过程中的所有信息

6、http://httpbin.org/ 这是一个请求接口的网站 ,就是你请求它就会将你的相关信息返回给你。我们可以试试请求如下:

curl http://httpbin.org/post

2022-05-11 

start

curl cip.cc 查看当前出口ip

end

curl -X POST  http://httpbin.org/post

如图:

undefined

7、curl -I http://httpbin.org/post  请求返回头信息,如图:

undefined

我们可以看一下返回头信息的作用,如图:

undefined

8、使用curl -d test=123  http://httpbin.org/post 携带参数进行请求,如图:

undefined

9、使用文件进行参数请求,在文件中编写a=1&b=2&c=3如图:

undefined

10、下载文件并以远程的文件名保存curl -O  http://httpbin.org/images/jpeg,如图:

undefined

11、跟随重定向请求 返回头信息curl -IL  https://baidu.com,如图:

undefined

12、设置请求头 curl -o image.webp -H "accept:image/webp" http://httpbin.org/image  设置为image/webp格式并保存为image.webp,如图:

undefined

13、设置cookies   curl -b a=test http://httpbin.org/cookies,如图:

undefined

14、查看相关的帮助,如图:

undefined

2024-10-09 start:

使用post 并加入header、body json 请求的实例:

curl -X POST \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer your_access_token" \ # 如果需要认证
     -d '{"id":"123","paramList":["1","2","3","4","5","6"]}' \
     http://your.target.url/path

end

你好:我的2025