php curl抓取网页指定内容(mancurl常用参数curl命令参数介绍)

优采云 发布时间: 2022-02-10 19:13

  php curl抓取网页指定内容(mancurl常用参数curl命令参数介绍)

  curl 是一款功能非常强大的工具软件,主要用于采集或上传数据。可以单独在控制台中使用,也可以在程序中使用。非常强大。

  使用说明

  curl命令是一个功能强大的网络工具,可以通过http、ftp等方式下载和上传文件。其实curl远不止上面提到的功能。您可以通过 man curl 阅读手册页以获取更多信息。 wget 是一个类似的工具。

  curl 命令是使用 libcurl 库实现的。 libcurl 库通常在 C 程序中用于处理 HTTP 请求。 curlpp 是 libcurl 的 C++ 包。这些东西可以用在web抓取、网络监控等开发中,curl命令可以帮助解决开发过程中遇到的问题。

  常用参数

  curl命令的参数很多,这里只列出我用过的,尤其是shell脚本中的。

  -A:请随意指定您自己为本次访问声明的浏览器信息

  -b/--cookie cookie 字符串或文件读取位置,使用 option 将最后一个 cookie 信息附加到 http 请求中。

  -c/--cookie-jar 操作完成后将cookies写入该文件

  -C/--continue-在断点处继续

  -d/--data HTTP POST方法传输数据

  -D/--dump-header 将头信息写入该文件

  -F/--form 模拟http表单提交数据

  -v/--verbose 小写的v参数用于打印更多信息,包括发送的请求信息,在调试脚本时特别有用。

  -m/--max-time 指定处理的最大持续时间

  -H/--header 指定请求头参数

  -s/--slent reduce输出信息,如进度

  --connect-timeout 指定尝试连接的最长时间

  -x/--代理

  指定代理服务器地址和端口,默认端口为1080

  -T/--upload-file 指定上传文件路径

  -o/--output 指定输出文件名

  --retry 指定重试次数

  -e/--referer 指定引用地址

  -I/--head 只返回header信息,使用HEAD请求

  -u/--user 设置服务器用户和密码

  -O:根据服务器上的文件名,会自动在本地存在

  -r/--range 从 HTTP/1.1 或 FTP 服务器检索字节范围

  -T/--upload-file 上传文件

  使用示例

  1、抓取页面内容到文件中

  [root@xi mytest]# curl -o home.html -- 将百度首页的内容抓取到home.html中

  [root@xi mytest]#curl -o #2_#1.jpg~{A,B}/[001-201].JPG

  因为A/B下的文件名都是001、002...、201,所以下载的文件同名,所以自定义下载的文件名变成这样: 原文:A/00< @1.JPG ---> 下载后:001-A.JPG 原文:B/001.JPG ---> 下载后:001-B.JPG

  2、使用-O(大写),后面的url必须特定于某个文件,否则不会被抓到。你也可以使用正则表达式来抓取东西

  [root@xi mytest]# curl -O

  结果如下:

  % Total % Received % Xferd 平均速度 时间 时间 时间 当前

  Dload 上传总花费的左速度

  100 1575 100 1575 0 0 14940 0 --:--:-- --:--:-- --:--:-- 1538k

  在当前执行目录中会生成一张bdlogo.gif的图片。

  [root@xi mytest]# curl -O[1-10].JPG --下载屏幕1.jpg~screen10.jpg

  3、模拟表单信息、模拟登录、保存cookie信息

  [root@xi mytest]# curl -c ./cookie_c.txt -F log=aaaa -F pwd=******

  4、模拟表单信息、模拟登录、保存表头信息

  [root@xi mytest]# curl -D ./cookie_D.txt -F log=aaaa -F pwd=******

  由

  生成的cookie

  -c(小写)与-D中的cookie不同。

  5、使用cookie文件

  [root@xi mytest]# curl -b ./cookie_c.txt

  6、从断点恢复,-C(大写)

  [root@xi mytest]# curl -C -O

  7、发送数据,最好使用登录页面测试,因为你传值后,curl会抓取数据回来,可以查看是否传值成功

  [root@xi mytest]# curl -d log=aaaa

  8、显示爬取错误,下面的例子清楚的显示出来。

  [root@xi mytest]# curl -f

  curl:(22)请求的URL返回错误:404

  [root@xi mytest]# curl

  404,未找到

  9。伪造源地址,有的网站会判断并请求源地址,防止盗链。

  [root@xi mytest]# curl -e

  10、当我们经常用curl做别人的事情的时候,人家会屏蔽你的IP,这个时候我们可以使用代理

  [root@xi mytest]# curl -x 24.10.28.84:32779 -o home.html

  11,对于较大的东西,我们可以分段下载

  [root@xi mytest]# curl -r 0-100 -o img.part1content/uploads/2010/09/compare_varnish.jpg

  % Total % Received % Xferd 平均速度 时间 时间 时间 当前

  Dload 上传总花费的左速度

  100 101 100 101 0 0 105 0 --:--:-- --:--:-- --:--:-- 0

  [root@xi mytest]# curl -r 100-200 -o img.part2ontent/uploads/2010/09/compare_varnish.jpg

  % Total % Received % Xferd 平均速度 时间 时间 时间 当前

  Dload 上传总花费的左速度

  100 101 100 101 0 0 57 0 0:00:01 0:00:01 --:--:-- 0

  [root@xi mytest]# curl -r 200- -o img.part3content/uploads/2010/09/compare_varnish.jpg

  % Total % Received % Xferd 平均速度 时间 时间 时间 当前

  Dload 上传总花费的左速度

  100 104k 100 104k 0 0 52793 0 0:00:02 0:00:02 --:--:-- 88961

  [root@xi mytest]# ls |grep part | xargs du -sh

  4.0K one.part1

  112K 三.part3

  4.0K two.part2

  使用的时候,cat一下就可以了,cat img.part* >img.jpg

  12,不会显示下载进度信息

  [root@xi mytest]# curl -s -o aaa.jpg

  13、显示下载进度条

  [root@xi mytest]# curl -0(使用http1.0协议的请求)

  ############################################## # ######################### 100.0%

  14、通过ftp下载文件

  [xifj@Xi ~]$ curl -u 用户名:密码 -O

  % Total % Received % Xferd 平均速度 时间 时间 时间 当前

  Dload 上传总花费的左速度

  101 1934 101 1934 0 0 3184 0 --:--:-- --:--:-- --:--:-- 7136

  [xifj@Xi ~]$ curl -u 用户名:密码 -O

  % Total % Received % Xferd 平均速度 时间 时间 时间 当前

  Dload 上传总花费的左速度

  101 1934 101 1934 0 0 3184 0 --:--:-- --:--:-- --:--:-- 7136

  或者使用下面的方法

  [xifj@Xi ~]$ curl -O ftp://username:password@ip:port/demo/curtain/bbstudy_files/style.css

  [xifj@Xi ~]$ curl -O ftp://username:password@ip:port/demo/curtain/bbstudy_files/style.css

  15、通过ftp上传

  [xifj@Xi ~]$ curl -T test.sql ftp://username:password@ip:port/demo/curtain/bbstudy_files/

  [xifj@Xi ~]$ curl -T test.sql ftp://username:password@ip:port/demo/curtain/bbstudy_files/

  15、模拟浏览器头部

  [xifj@Xi ~]$ curl -A "Mozilla/4.0 (兼容; MSIE 6.0; Windows NT 5.0)" -x 12< @3.45.67.89:1080 -o page.html -D cookie0001.txt

  16,PUT,GET,POST

  比如curl -T localfile~zz/abc.cgi,此时使用的协议是HTTP PUT方式

  刚才说到PUT,我自然会想到其他几种方法——GET和POST。

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线