基于curl实现登录页面数据采集(百度搜藏版)

本文以百度搜藏为例,提供一种基于curl采集登录页数据的方法。

  1. 获取cURL命令

    这个可以借助Firebug的控制台得到,如图(一)所示: 
    基于Firebug获取cURL命令

    1. 解析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)



  1. <?php
  2. header("Content-type: text/html; charset=utf-8");
  3. require_once 'CurlMulti.php';
  4. require_once 'MyCurl.php';
  5. /**
  6. * 抓取百度搜藏数据
  7. * @author Zjmainstay
  8. * @website http://www.zjmainstay.cn
  9. * @year 2014
  10. * @usage
  11. cli命令: php curlBaiduSoucang.php 1 20 //1~20页
  12. 浏览器: http://localhost/curlmulti/curlBaiduSoucang.php?start=1&end=20
  13. *
  14. */
  15. class curlBaiduSoucang extends MyCurl {
  16. function __construct() {
  17. parent::__construct();
  18. $curlTextFile = dirname(__FILE__) . '/curlText.txt';
  19. if(!file_exists($curlTextFile)) {
  20. exit('curl 命令文件(./curlText.txt)不存在。');
  21. }
  22. $curlContent = trim(file_get_contents($curlTextFile));
  23. if(!preg_match("#curl '([^']*?)'#is", $curlContent, $match)) {
  24. exit('请确认curl命令是否正确,无法匹配链接地址。');
  25. }
  26. $this->soucangUrl = $match[1];
  27. if(!preg_match_all("#-H '([^']*?)'#is", $curlContent, $matches)) {
  28. echo('请确认curl命令是否正确,无法匹配HTTP HEADER信息,可能导致采集失败
  29. ');
  30. } else {
  31. $httpHeader = $matches[1];
  32. }
  33. $this->curl->opt[CURLOPT_ENCODING] = 'gzip';
  34. if(!empty($httpHeader)) {
  35. $this->curl->opt[CURLOPT_HTTPHEADER] = $httpHeader;
  36. }
  37. }
  38. //采集开始处理
  39. function run() {
  40. if(PHP_SAPI=='cli') {
  41. global $argv;
  42. $this->lineBreak = "\n";
  43. } else {
  44. $this->lineBreak = "<br />";
  45. $argv[2] = (int)@$_GET['start'];
  46. $argv[3] = (int)@$_GET['end'];
  47. }
  48. $startPage = max(1, (int)@$argv[2]);
  49. $endPage = max(1, (int)@$argv[3]);
  50. $process = array (
  51. $this,
  52. 'parse'
  53. );
  54. for($i = $startPage; $i <= $endPage; $i++) {
  55. $this->curl->add ( array (
  56. 'url' => preg_replace('#(?<=pn=)\d+#i', $i, $this->soucangUrl),
  57. 'args' => array (
  58. //args
  59. 'page' => $i,
  60. )
  61. ), $process );
  62. }
  63. $this->curl->start();
  64. }
  65. function parse($res, $param) {
  66. if (! $this->httpError ( $res ['info'] )) {
  67. $filename = dirname(__FILE__) . "/soucang/soucang-{$param['page']}.txt";
  68. file_put_contents($filename, trim(iconv('gbk', 'utf-8//IGNORE', $res['content'])));
  69. }
  70. echo "Page: {$param['page']} (ok)" . $this->lineBreak;
  71. }
  72. function cbCurlInfo($info) {
  73. parent::cbCurlInfo($info);
  74. echo $this->lineBreak;
  75. }
  76. }
  77. ini_set('max_execution_time', 0);
  78. ini_set('display_errors', 'on');
  79. error_reporting(E_ALL);
  80. $curlObj = new curlBaiduSoucang;
  81. $curlObj->run();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值