php网页爬虫
<?php
/*网页爬虫*/
#最简单的使用,属性都采用默认值
/*
$curl=curl_init(‘http://www.baidu.com‘);
$output=curl_exec($curl);
curl_close($curl);
echo $output;
*/
#稍微复杂一点的,对页面进行操作
/*
$curl=curl_init();
curl_setopt($curl, CURLOPT_URL, ‘http://www.baidu.com‘);//可以动态改变网址
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//不直接打印在浏览器上
$output=curl_exec($curl);//执行
curl_close($curl);//关闭
echo str_replace(‘百度‘, ‘虎哥‘, $output);
*/
#更复杂一点的,模拟POST请求
/*
$data=‘theCityName=北京‘;
$curl=curl_init();
curl_setopt($curl, CURLOPT_URL,‘http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName‘);
curl_setopt($curl,CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
curl_setopt($curl,CURLOPT_HTTPHEADER,array(‘application/x-www-form-rulencode;charset=utf-8‘,‘Content-length‘.strlen($data)));
$output=curl_exec($curl);
if(!curl_errno($curl)){
//$info=curl_getinfo($curl);
//print_r($info);
header(‘Content-type:text/html;charset=utf-8;‘);
echo $output;
}else{
echo ‘Curl Error:‘.curl_error($curl);
}
curl_close($curl);
*/
#抓取需要登录的页面
/*
$data=‘username=[email protected]&password=19900417&remember=1‘;
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,‘http://www.imooc.com/user/login‘);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);//执行之后不直接打印出来
//Cookie相关设置,这部分设置需要在所有回话开始之前设置
date_default_timezone_set(‘PRC‘);//使用cookie时必须设置时区
curl_setopt($curl,CURLOPT_COOKIESESSION, true);
curl_setopt($curl,CURLOPT_COOKIEFILE,‘cookiefile‘);
curl_setopt($curl,CURLOPT_COOKIEJAR,‘cookiefile‘);
curl_setopt($curl,CURLOPT_COOKIE, session_name().‘=‘.session_id());
curl_setopt($curl,CURLOPT_HEADER,0);//禁止输出头信息
curl_setopt($curl,CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $data);
curl_setopt($curl,CURLOPT_HTTPHEADER,array(‘application/x-www-form-rulencode;charset=utf-8‘,‘Content-length:‘.strlen($data)));
curl_exec($curl);//执行
curl_setopt($curl,CURLOPT_URL,‘http://www.imooc.com/space/index‘);
curl_setopt($curl,CURLOPT_POST,0);//下载页面不是post请求,故置0
curl_setopt($curl,CURLOPT_HTTPHEADER,array(‘Content-type:text/xml‘));
$output=curl_exec($curl);
echo $output;
*/
#从ftp服务器上下载一个文件
/*
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,‘ftp://10.14.12.22:21/test.txt‘);
curl_setopt($curl,CURLOPT_HEADER,0);//禁止输出头信息,调试时,可以输出
curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_TIMEOUT,300);//设置超时时间,单位秒
curl_setopt($curl,CURLOPT_USERPWD,"admin:123");//ftp用户名,密码
$outfile=fopen(‘mytest.txt‘, ‘wb‘);//保存到本地的文件名
curl_setopt($curl,CURLOPT_FILE,$outfile);
$output=curl_exec($curl);
fclose($outfile);
if(!curl_errno($curl)){
//$info=curl_getinfo($curl);
//print_r($info);
echo "RETURN:".$output;
}else{
echo ‘Curl error:‘.curl_error($curl);
}
curl_close($curl);
*/
#ftp文件上传
/*
$curl=curl_init();
$uploadfile=‘demo1.php‘;
$fp=fopen($uploadfile,‘r‘);
curl_setopt($curl, CURLOPT_URL, ‘ftp://10.14.12.22:21/uploadfile.php‘);
curl_setopt($curl, CURLOPT_HEADER,0);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_TIMEOUT,300);
curl_setopt($curl,CURLOPT_USERPWD,‘admin:123‘);
curl_setopt($curl,CURLOPT_UPLOAD,1);
curl_setopt($curl, CURLOPT_INFILE, $fp);
curl_setopt($curl,CURLOPT_INFILESIZE,filesize($uploadfile));
$output=curl_exec($curl);
fclose($fp);
if(!curl_errno($curl)){
echo ‘uploadfile successfully‘;
}else{
echo ‘uploadfile error:‘.curl_error($curl);
}
curl_close($curl);
*/
#curl访问https上的资源
/*
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,‘https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js‘);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
date_default_timezone_set(‘PRC‘);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);//终止从服务端进行验证
$output=curl_exec($curl);
curl_close($curl);
echo $output;
*/
$data=‘{
"button": [
{
"name": "我要淘书",
"sub_button": [
{
"type": "click",
"name": "注册账号",
"key": "zcyh"
},
{
"type": "click",
"name": "账号绑定",
"key": "bd"
},
{
"type": "click",
"name": "图书搜索",
"key": "tsss"
},
{
"type": "click",
"name": "卖书订单",
"key": "cx@sell"
},
{
"type": "click",
"name": "买书订单",
"key": "cx@buy"
}
]
},
{
"name": "努力学习",
"sub_button": [
{
"type": "click",
"name": "成绩查询",
"key": "cjcx"
},
{
"type": "click",
"name": "考场查询",
"key": "kccx"
},
{
"type": "click",
"name": "课表查询",
"key": "kbcx"
}
]
},
{
"name": "多彩生活",
"sub_button": [
{
"type": "click",
"name": "学霸工具",
"key":"xbtool"
},
{
"type": "view",
"name": "校园周边",
"url":"http://weixuebar.duapp.com/View/surround/schoolSurround.html"
},
{
"type": "click",
"name": "在线翻译",
"key": "zxfy"
},
{
"type": "click",
"name": "学霸社区",
"key": "xbsq"
},
{
"type": "view",
"name": "联系我们",
"url": "http://weixuebar.duapp.com/View/help.html"
}
]
}
]
}‘;
$url=‘https://api.weixin.qq.com/cgi-bin/menu/create?access_token=4__FZWKzO1gP2_PhvUE9F9_1rpWy2whbQUfKIuvRNBQHnfDrfANGRogi0RXYSwxUW2CYkrfGlKtB5I5wTOOLoA‘;
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
date_default_timezone_set(‘PRC‘);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);//终止从服务端进行验证
curl_setopt($curl,CURLOPT_HTTPHEADER,array(‘application/x-www-form-rulencode;charset=utf-8‘,‘Content-length‘.strlen($data)));
$output=curl_exec($curl);
curl_close($curl);
echo $output;
/*curl_setopt($curl, CURLOPT_URL,‘http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName‘);
curl_setopt($curl,CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
curl_setopt($curl,CURLOPT_HTTPHEADER,array(‘application/x-www-form-rulencode;charset=utf-8‘,‘Content-length‘.strlen($data)));
*/
//php官方文档网址:http://php.net/manual/zh/book.curl.php
?>
本文出自 “虎哥的博客” 博客,请务必保留此出处http://7613577.blog.51cto.com/7603577/1531692
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。