php爬虫神器cURL
cURL
网页资源(编写网页爬虫)
接口资源
ftp服务器文件资源
其他资源

static public function curl($url, $data = array(), $timeout = 5) {
$ch = curl_init ();
if (is_array ( $data ) && $data) {
// http_build_query — 生成 URL-encode 之后的请求字符串,支持数组提交
$formdata = http_build_query ( $data );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $formdata );
}
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result;
}
下面是简单的爬虫,爬网页数据。
<?php
/**
* 简单爬虫
*/
$ch = curl_init('http://www.baidu.com');
curl_exec($ch);
curl_close($ch);
替换爬出来的网页数据。
<?php
/**
* 简单爬虫
*/
$curlobj = curl_init();
curl_setopt($curlobj,CURLOPT_URL,"http://www.baidu.com"); // 设置抓取的网页
curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,true); // 设置不打印
$output=curl_exec($curlobj); // 执行
curl_close($curlobj); // 关闭
echo str_replace("百度","搜索",$output);
post调接口数据
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather

<?php
/**
* 简单爬虫
*/
$curlobj = curl_init();
$url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather";
$data= "theCityCode=宿迁&theUserID=6c0fbb1189324dfab6a66963738d768b"; // 麻痹,这个只能试用五天
curl_setopt($curlobj,CURLOPT_URL,$url); // 设置抓取的网页
curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,true); // 设置不打印
curl_setopt($curlobj,CURLOPT_POST,1); // 设置post
curl_setopt($curlobj,CURLOPT_POSTFIELDS,$data); // 设置post数据
$output=curl_exec($curlobj); // 执行
if(!curl_errno($curlobj)){ // 返回错误代码或在没有错误发生时返回 0 (零)。
echo $output;
} else {
echo 'Curl error:'.curl_error($curlobj);
}
curl_close($curlobj); // 关闭
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/">
<string>江苏 宿迁</string>
<string>宿迁</string>
<string>1907</string>
<string>2018/04/26 22:10:44</string>
<string>今日天气实况:气温:18℃;风向/风力:西南风 1级;湿度:78%</string>
<string>紫外线强度:弱。空气质量:良。</string>
<string>紫外线指数:弱,辐射较弱,涂擦SPF12-15、PA+护肤品。
健臻·血糖指数:较易波动,血糖较易波动,注意监测。
感冒指数:较易发,温差较大,较易感冒,注意防护。
穿衣指数:较舒适,建议穿薄外套或牛仔裤等服装。
洗车指数:较适宜,无雨且风力较小,易保持清洁度。
空气污染指数:良,气象条件有利于空气污染物扩散。
</string>
<string>4月26日 多云</string>
<string>13℃/23℃</string>
<string>南风3-4级</string>
<string>1.gif</string>
<string>1.gif</string>
<string>4月27日 多云</string>
<string>11℃/26℃</string>
<string>东北风转东南风3-4级</string>
<string>1.gif</string>
<string>1.gif</string>
<string>4月28日 多云</string>
<string>17℃/27℃</string>
<string>东南风3-4级</string>
<string>1.gif</string>
<string>1.gif</string>
<string>4月29日 多云</string>
<string>19℃/28℃</string>
<string>东南风3-4级</string>
<string>1.gif</string>
<string>1.gif</string>
<string>4月30日 多云</string>
<string>19℃/30℃</string>
<string>东南风转东风3-4级</string>
<string>1.gif</string>
<string>1.gif</string>
<string>5月1日 小雨</string>
<string>17℃/26℃</string>
<string>东北风4-5级</string>
<string>7.gif</string>
<string>7.gif</string>
<string>5月2日 多云转阴</string>
<string>11℃/27℃</string>
<string>东北风4-5级转无持续风向小于3级</string>
<string>1.gif</string>
<string>2.gif</string>
</ArrayOfString>
读取FTP数据
<?php
/**
* 简单爬虫
*/
$curlobj = curl_init();
$url = "ftp://192.168.199.126/info.txt";
curl_setopt($curlobj,CURLOPT_URL,$url); // 设置抓取的网页
curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,true); // 设置不打印
curl_setopt($curlobj,CURLOPT_HEADER,0);
curl_setopt($curlobj,CURLOPT_TIMEOUT,300); // 设置300秒的下载时间
$outfile = fopen('download_file.txt','wb'); // 保存到本地的文件名
curl_setopt($curlobj,CURLOPT_FILE,$outfile);
$r=curl_exec($curlobj); // 执行
fclose($outfile);
if(!curl_errno($curlobj)){ // 返回错误代码或在没有错误发生时返回 0 (零)。
echo "RETURN:" .$r;
} else {
echo 'Curl error:'.curl_error($curlobj);
}
curl_close($curlobj); // 关闭
RETURN:1
<?php
/**
* 简单爬虫
*/
$curlobj = curl_init();
$url = "ftp://192.168.199.126/info.txt";
curl_setopt($curlobj,CURLOPT_URL,$url); // 设置抓取的网页
curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,true); // 设置不打印
curl_setopt($curlobj,CURLOPT_HEADER,0);
curl_setopt($curlobj,CURLOPT_TIMEOUT,300); // 设置300秒的下载时间
//$outfile = fopen('download_file.txt','wb'); // 保存到本地的文件名
//curl_setopt($curlobj,CURLOPT_FILE,$outfile);
$r=curl_exec($curlobj); // 执行
//fclose($outfile);
if(!curl_errno($curlobj)){ // 返回错误代码或在没有错误发生时返回 0 (零)。
echo "RETURN:" .$r;
} else {
echo 'Curl error:'.curl_error($curlobj);
}
curl_close($curlobj); // 关闭
RETURN:hello world!
上传FTP数据
<?php
/**
* 简单爬虫
*/
$curlobj = curl_init();
$localfile = 'upload_file.txt';
$fp = fopen($localfile,'r');
$url = "ftp://192.168.199.126/upload/for_upload_file.txt"; // 确保目录有写权限
curl_setopt($curlobj,CURLOPT_URL,$url); // 设置抓取的网页
curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,true); // 设置不打印
curl_setopt($curlobj,CURLOPT_HEADER,0);
curl_setopt($curlobj,CURLOPT_TIMEOUT,300); // 设置300秒的下载时间
curl_setopt($curlobj,CURLOPT_UPLOAD,1);
curl_setopt($curlobj,CURLOPT_INFILE,$fp);
curl_setopt($curlobj,CURLOPT_INFILESIZE,filesize($localfile));
$r=curl_exec($curlobj); // 执行
fclose($fp);
if(!curl_errno($curlobj)){ // 返回错误代码或在没有错误发生时返回 0 (零)。
echo "RETURN:上传成功!";
} else {
echo 'Curl error:'.curl_error($curlobj);
}
curl_close($curlobj); // 关闭
还是很牛逼的,常用于post提交获取数据和爬虫获取资源。
php爬虫神器cURL的更多相关文章
- shell神器curl命令的用法 curl用法实例笔记
shell神器curl命令的用法举例,如下: ##基本用法(配合sed/awk/grep) $curl http://www.jquerycn.cn ##下载保存 $curl http://www.j ...
- 爬虫神器XPath,程序员带你免费获取周星驰等明星热门电影
本教程由"做全栈攻城狮"原创首发,本人大学生一枚平时还需要上课,但尽量每日更新文章教程.一方面把我所习得的知识分享出来,希望能对初学者有所帮助.另一方面总结自己所学,以备以后查看. ...
- cmder 神器 +curl
cmder 神器 https://www.jianshu.com/p/7a706c0a3411 curl https://www.cnblogs.com/zhuzhenwei918/p/6781314 ...
- Python爬虫利器 cURL你用过吗?
hello,小伙伴们,今天给大家分享的开源项目是一个python爬虫利器,感兴趣的小伙伴看完这篇文章不妨去尝试一下,这个开源项目就是curlconverter,不知道小伙伴们分析完整个网站后去code ...
- 网络爬虫2--PHP/CURL库(client URL Request Library)
PHP/CURL库功能 多种传输协议.CURL(client URL Request Library),含义是“客户端URL请求库”. 不像上一篇所用的PHP内置网络函数,PHP/CURL支持多种 ...
- python爬虫神器PyQuery的使用方法
你是否觉得 XPath 的用法多少有点晦涩难记呢? 你是否觉得 BeautifulSoup 的语法多少有些悭吝难懂呢? 你是否甚至还在苦苦研究正则表达式却因为少些了一个点而抓狂呢? 你是否已经有了一些 ...
- 爬虫神器xpath的用法(三)
xpath的多线程爬虫 #encoding=utf-8 ''' pool = Pool(4) cpu的核数为4核 results = pool.map(爬取函数,网址列表) ''' from mult ...
- App爬虫神器mitmproxy和mitmdump的使用
原文 mitmproxy是一个支持HTTP和HTTPS的抓包程序,有类似Fiddler.Charles的功能,只不过它是一个控制台的形式操作. mitmproxy还有两个关联组件.一个是mitmdum ...
- 爬虫神器xpath的用法(四)
使用xpath多线程爬取百度贴吧内容 #encoing=utf-8 from lxml import etree from multiprocessing.dummy import Pool as T ...
随机推荐
- C++ 进阶学习 ——模板
模板和重载类似,比重载更省事 通常有两种形式:函数模板和类模板: 函数模板针对仅参数类型不同的函数: 类模板针对仅数据成员和成员函数类型不同的类. 一个简单的函数模板 template <cla ...
- python学习笔记(unittest)
刚刚放假回来我想很多人都还没有缓过来吧 这次介绍一个python自带的测试框架 unitest #!/usr/bin/env python # -*- coding: utf_8 -*- import ...
- Python之virtualenv沙盒环境
在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-packages目录下. 如果我们要同时开发多个应用程序,那这 ...
- Project Euler 126 - Cuboid layers
这题先是推公式… 狂用不完全归纳+二次回归,最后推出这么一个奇怪的公式 \[f(t,x,y,z)=4(t-1)(x+y+z+t-2)+2(xy+yz+xz)\] 表示长宽高为\(x\).\(y\).\ ...
- sphinx使用
一. 1.先得包含下载的文件 include'./sphinx/api/sphinxapi.php'; $sphinx= new SphinxClient(); $sphinx->SetServ ...
- apply 无循环拼接数组
apply()第二个参数只能是数组,这个数组将作为参数传给原函数的参数列表arguments. 其实在实际开发中,JS 继承的方法并不止这一种,使用原型链继承是更加常用的方式,此外还有构造函数继承,这 ...
- 移动元素时,translate要比margin好
比如 做全屏轮播时,父元素往往是被子元素撑起来的,那你设置父元素的margin时,往往会感染到子元素,如下图: 而用translate3d就不会出现这种效果:
- verilog case 语句合并问题
有时候在case语句中会有不同选择执行相同操作的情况,为了简化代码,可以将其合并. 以下解答来自百度知道(由于排版问题,有相应修改): reg [1:0]addr_cnt=2'b11; reg rea ...
- SGU 502 Digits Permutation
这个题目 正解应该是 dp 吧 对18个数字进行2进制枚举放不放,,,可以这么理解 以当前状态 stu,他对应的余数是 h 进入下一个状态: 进行记忆画搜索就行了 1 #include<iost ...
- python之Beautiful Soup库
1.简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索 ...