基于curl实现登录页面数据采集(百度搜藏版)
来源:互联网 发布:ubuntu 商业版 编辑:程序博客网 时间:2024/05/29 17:52
本文以百度搜藏为例,提供一种基于curl采集登录页数据的方法。
获取cURL命令
这个可以借助Firebug的控制台得到,如图(一)所示:
- 解析cURL命令
cURL具有一定的格式,我们这里需要得到两个数据:
(1)请求链接
它可以基于正则"#curl '([^']*?)'#is"
解析得到,唯一,使用preg_match匹配。
(2)请求头HTTPHEADER
它可以基于正则"#-H '([^']*?)'#is"
得到,一个或多个,基于preg_match_all匹配。
3. 通过cURL采集数据
本demo基于php多线程采集实现,此时只需要给curl采集请求添加:
(1)压缩参数
CURLOPT_ENCODING="gzip"
(2)HTTP头参数
CURLOPT_HTTPHEADER=cURL解析头结果
4. 程序核心源码(curlBaiduSoucang.php)
<?php
header("Content-type: text/html; charset=utf-8");
require_once'CurlMulti.php';
require_once'MyCurl.php';
/**
* 抓取百度搜藏数据
* @author Zjmainstay
* @website http://www.zjmainstay.cn
* @year 2014
* @usage
cli命令: php curlBaiduSoucang.php 1 20 //1~20页
浏览器: http://localhost/curlmulti/curlBaiduSoucang.php?start=1&end=20
*
*/
class curlBaiduSoucang extendsMyCurl{
function __construct(){
parent::__construct();
$curlTextFile= dirname(__FILE__).'/curlText.txt';
if(!file_exists($curlTextFile)){
exit('curl 命令文件(./curlText.txt)不存在。');
}
$curlContent= trim(file_get_contents($curlTextFile));
if(!preg_match("#curl '([^']*?)'#is", $curlContent, $match)){
exit('请确认curl命令是否正确,无法匹配链接地址。');
}
$this->soucangUrl= $match[1];
if(!preg_match_all("#-H '([^']*?)'#is", $curlContent, $matches)){
echo('请确认curl命令是否正确,无法匹配HTTP HEADER信息,可能导致采集失败
');
}else{
$httpHeader= $matches[1];
}
$this->curl->opt[CURLOPT_ENCODING]='gzip';
if(!empty($httpHeader)){
$this->curl->opt[CURLOPT_HTTPHEADER]= $httpHeader;
}
}
//采集开始处理
function run(){
if(PHP_SAPI=='cli'){
global $argv;
$this->lineBreak="\n";
}else{
$this->lineBreak="<br />";
$argv[2]=(int)@$_GET['start'];
$argv[3]=(int)@$_GET['end'];
}
$startPage= max(1,(int)@$argv[2]);
$endPage= max(1,(int)@$argv[3]);
$process= array(
$this,
'parse'
);
for($i= $startPage; $i <= $endPage; $i++){
$this->curl->add( array(
'url'=> preg_replace('#(?<=pn=)\d+#i', $i, $this->soucangUrl),
'args'=> array(
//args
'page'=> $i,
)
), $process );
}
$this->curl->start();
}
function parse($res, $param){
if(! $this->httpError( $res['info'])){
$filename= dirname(__FILE__)."/soucang/soucang-{$param['page']}.txt";
file_put_contents($filename, trim(iconv('gbk','utf-8//IGNORE', $res['content'])));
}
echo"Page: {$param['page']} (ok)". $this->lineBreak;
}
function cbCurlInfo($info){
parent::cbCurlInfo($info);
echo $this->lineBreak;
}
}
ini_set('max_execution_time',0);
ini_set('display_errors','on');
error_reporting(E_ALL);
$curlObj=new curlBaiduSoucang;
$curlObj->run();
0 0
- 基于curl实现登录页面数据采集(百度搜藏版)
- curl登录采集
- curl自动登录CNZZ获得数据(数据采集)
- curl采集循环数据
- curl 采集ssl证书页面
- 基于PHP的cURL入门教程 (小偷采集程序)
- php 模拟登录淘宝taobao阿里妈妈|模拟登录淘宝联盟|curl模拟登录淘宝|模拟登陆淘宝采集数据
- php curl实现多线程采集
- 基于HtmlUnit实现简单登录、页面跳转以及获取有用数据部分代码示例(示例网站:大润发)
- 基于V4L2采集数据
- 百度登录页面
- PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发
- PHP:防止跨域CURL采集数据
- CURL使用示例之数据采集
- curl 采集微博信息(模拟登录新浪微博)
- 基于java数据采集串口通讯的设计和实现
- 基于Mina实现的一个简单数据采集中间件
- python数据采集百度知道
- 天声人語 20150826
- 黑马程序员——C语言中的注释
- jxl(Java Excel API) 使用方法 【1】
- 静态分析中”Potential null dereference”的处理
- Web前端和后端
- 基于curl实现登录页面数据采集(百度搜藏版)
- VS2008配置SVN
- 日经春秋 20150826
- jquery制作banner图片左右上下切换滚动特效
- 【Leetcode】Jump Game 1,2
- 网易新闻iOS版使用的18个开源组件
- 分布式数据库集群中间件------为什么选择MySQL
- 日经社説 20150826 ネット国勢調査を円滑に
- Learning C/C++