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 ...
随机推荐
- ReflectionZ_测试_01
1.Java代码 public class TreflectionZ { public static void main(String[] args) throws Exception { Class ...
- es6 中的let,const
在es6中,let的作用和var差不多,都是用来声明变量的,但是他们之间的区别在于作用域不同,大家都知道在js中没有块级作用域,例如: for(var i=0;i<10;i++){ consol ...
- python基础7 - 函数
1. 函数的快速体验 所谓函数,就是把 具有独立功能的代码块 组织为一个小模块,在需要的时候 调用 函数的使用包含两个步骤: 定义函数 —— 封装 独立的功能 调用函数 —— 享受 封装 的成果 函数 ...
- selenium学习笔记(xpath和css定位)
简单的介绍下xpath和css的定位 理论知识就不罗列了 还是利用博客园的首页.直接附上代码: 这个是xpath #!/usr/bin/env python # -*- coding: utf_8 - ...
- python中的mysql操作
一. 数据库在自动化测试中的应用 存测试数据 有的时候大批量的数据,我们需要存到数据库中,在测试的时候才能用到,测试的时候就从数据库中读取出来.这点是非常重要的! 存测试结果 二. python中的数 ...
- 20-THREE.JS 混合材质
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- 版本工具管理之----git
如何查看隐藏文件夹: 如果你看不到.git目录,你需要让隐藏的文件可见.具体做法就是打开一个Terminal窗口,输入以下命令: defaults write com.apple.finder App ...
- Render QGraphicsItem on QPixmap: aggregate 'QWidget w' has incomplete type and cannot be defined
Render QGraphicsItem on QPixmap: aggregate 'QWidget w' has incomplete type and cannot be defined #in ...
- Precision/Recall、ROC/AUC、AP/MAP等概念区分
1. Precision和Recall Precision,准确率/查准率.Recall,召回率/查全率.这两个指标分别以两个角度衡量分类系统的准确率. 例如,有一个池塘,里面共有1000条鱼,含10 ...
- make: *** No rule to make target `out/target/common/obj/APPS/framework-res_intermediates/src/R.stamp'
/********************************************************************************** * make: *** No r ...