分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)


Posted in PHP onJanuary 30, 2014
<?php
/**
 * HOST: www.icbase.com
 */
//set_time_limit(0);
// base function
function curl_get($url, $data = array(), $header = array(), $timeout = 15, $port = 80, $reffer = '', $proxy = '')
{
 $ch = curl_init();
 if (!empty($data)) {
 $data = is_array($data)?http_build_query($data): $data;
 $url .= (strpos($url,'?')? '&': "?") . $data;
 }
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 curl_setopt($ch, CURLOPT_POST, 0);
 curl_setopt($ch, CURLOPT_PORT, $port);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //是否抓取跳转后的页面
 $reffer && curl_setopt($ch, CURLOPT_REFERER, $reffer);
 if($proxy) {
 curl_setopt($ch, CURLOPT_PROXY, $proxy);
 curl_setopt($ch, CURLOPT_PROXYPORT, 1723);
 curl_setopt($ch, CURLOPT_PROXYUSERPWD,"andhm001:andhm123");
 }$result = array();
 $result['result'] = curl_exec($ch);
 if (0 != curl_errno($ch)) {
 $result['error'] = "Error:\n" . curl_error($ch);
}
 curl_close($ch);
 return $result;
}
function curl_post($url, $data = array(), $header = array(), $timeout = 5, $port = 80)
{
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 //curl_setopt($ch, CURLOPT_PORT, $port);
 !empty ($header) && curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);$result = array();
 $result['result'] = curl_exec($ch);
 if (0 != curl_errno($ch)) {
 $result['error'] = "Error:\n" . curl_error($ch);
}
 curl_close($ch);
return $result;
}
/**
 * 获取列表页的html源码
 * @param string $keywords 搜索关键字
 * @param int $page 页数
 * @return boolean|array
 */
function getListHtml($keywords, $page=1)
{
 if ($page < 0)
 {
 return false;
 }
 $page = $page == 0 ? 1 : intval($page);
 if ($page == 1)
 {
 $result = curl_get('http://www.icbase.com/ProResult.aspx', array('ProKey' => $keywords));
 if ( isset($result['error']) )
 {
 return false;
 //exit($result['error']);
 }
 $result = $result['result'];
 // asp.net post提交数据
 if(! defined('__VIEWSTATE') && preg_match('/<input\s+type="hidden"\s+name="__VIEWSTATE"\s+id="__VIEWSTATE"\s+value="(.[^"]+)"/isU', $result, $matches))
 {
 define('__VIEWSTATE', $matches[1]);
 } else {
 return false;
 }
 if(! defined('__PREVIOUSPAGE') && preg_match('/<input\s+type="hidden"\s+name="__PREVIOUSPAGE"\s+id="__PREVIOUSPAGE"\s+value="(.[^"]+)"/isU', $result, $matches))
 {
 define('__PREVIOUSPAGE', $matches[1]);
 } else {
 return false;
 }
 if(! defined('__EVENTVALIDATION') && preg_match('/<input\s+type="hidden"\s+name="__EVENTVALIDATION"\s+id="__EVENTVALIDATION"\s+value="(.[^"]+)"/isU', $result, $matches))
 {
 define('__EVENTVALIDATION', $matches[1]);
 } else {
 return false;
 }
 return $result;
 }
 $data = array(
 '__EVENTTARGET' => 'pager',
 '__EVENTARGUMENT' => $page,
 '__VIEWSTATE' => __VIEWSTATE,
 '__PREVIOUSPAGE' => __PREVIOUSPAGE,
 '__EVENTVALIDATION' => __EVENTVALIDATION,
 );
 $result = curl_post('http://www.icbase.com/ProResult.aspx?ProKey=' . $keywords, $data);
 if ( isset($result['error']) )
 {
 return false;
 //exit($result['error']);
 }
 $result = $result['result'];
 return $result;
}
/**
 * 获取列表页 a链接的url
 * @param string $html html源码
 * @return array
 */
function getListHref($html)
{
 $pattern = '/<a\s+href=\'(.[^\']+)\'\s+target="_blank"\s*>[\s\n]*<img.+[^>]\/>/isU';
 if (preg_match_all($pattern, $html, $matches))
 {
 return $matches[1];
 } else {
 // 没有匹配项
 return array();
 }
}
/**
 * 获取下一页数字
 * @param string $html html源码
 * @return number
 */
function getListNextPage($html)
{
 $pattern = '/<div\s+id="Pager".+[^>]>.+<a\s+href="javascript\:__doPostBack\(\'Pager\',\'(\d+)\'\)">><\/a>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 return intval($matches[1]);
 } else {
 return -1;
 }
}
/**
 * 获取列表也所有的href
 * @param string $keywords 搜索关键字
 * @return boolean|array
 */
function getListHrefAll($keywords)
{
 if (empty($keywords))
 {
 return false;
 }
 $html = getListHtml($keywords);
 $hrefList = getListHref($html);
 if (empty($hrefList))
 {
 // 没有结果
 return array();
 }
 $nextPage = getListNextPage($html);
 while ($nextPage > 0)
 {
 $html = getListHtml($keywords, $nextPage);
 $tmpHrefList = getListHref($html);
 $hrefList = array_merge($hrefList, $tmpHrefList);
 $nextPage = getListNextPage($html);
 }
 return $hrefList;
}
/**
 * 获取详情页信息
 * @param string $url url地址或者是抓取到的html源代码 根据@see $is_url 区分
 * @param int $is_url 1使用的是url地址 0直接处理html源代码
 * @return boolean|multitype:|multitype:string
 */
function getDetail($url, $is_url = 1)
{
 if ( empty($url) )
 {
 return false;
 }
 $host = 'www.icbase.com';
 $html = $url;
 if ($is_url) {
 $url = '/' . ltrim($url, '/');
 $result = curl_get($host . $url);
 if ( isset($result['error']) )
 {
 exit($result['error']);
 }
 $html = $result['result'];
 }
 $result = array(
 'sup_part' => '', // 供应商型号
 'sup_id' => '', // 供应商ID
 'mfg_part' => '', // 制造商型号
 'mfg_name' => '', // 制造商名称
 'cat_name' => '', // 分类名称
 'para' => '', // 属性
 'desc' => '', // 描述
 'pdf_url' => '', // PDF地址
 'sup_stock' => '', // 库存
 'min_purch' => '', // 最小订购量
 'price' => '', // 价格
 'img_url' => '', // 图片地址
 'createtime' => '', // 创建时间
 'datacode' => '', // 批号
 'package' => '', // 封装
 'page_url' => '', // 页面地址
 );
// mfg_part
 $pattern = '/<td>产品型号<\/td><td>(.[^<]+)</isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['mfg_part'] = trim($matches[1]);
 } else {
 // 此项木有,说明也没处处了
 return array();
 }
 // mfg_name
 $pattern = '/<td>厂商<\/td>[\s\n]*<td>(.+)<\/td>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['mfg_name'] = trim($matches[1]);
 }
 // para
 $pattern = '/<tr\s+style="background-color:#E9E9E9;color:black; font-weight:bold;">(.+)<\/tr><\/table>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 if (preg_match_all('/<td>(.+)<\/td>/isU', $matches[1], $matches))
 {
 $count = count($matches[1]);
 $count = intval($count / 2 );
 foreach ($matches[1] as $k=>$v)
 {
 if ($k >= $count)
 {
 break;
 }
 if (trim($v) == '描述')
 {
 // desc
 $result['desc'] = trim($matches[1][$count + $k]);
 continue;
 }
 $v = trim($v);
 $result['para'][$v] = trim($matches[1][$count + $k]);
 }
 }
 }
 // pdf_url
 $pattern = '/<td>详细资料<\/td><td><a\s+href="(.[^"]+)"/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['pdf_url'] = trim($matches[1]);
 }
 // sup_stock
 $pattern = '/<td>库存数量<\/td>[\s\n]*<td>(\d+)<\/td>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['sup_stock'] = trim($matches[1]);
 }
 // price
 $pattern = '/<tr><td.[^>]+>(\d+)\+<\/td><td.[^>]+>.[^\d]*([\d.]+)<\/td><\/tr>/isU';
 if (preg_match_all($pattern, $html, $matches))
 {
 foreach ($matches[1] as $k=>$v)
 {
 $result['price'][$v] = '¥' . $matches[2][$k];
 }
 }
 //img_url
 $pattern = '/<td>图片<\/td><td><img\s+src="(.[^"]+)"/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['img_url'] = trim($matches[1]);
 }
 // page_url
 if ($is_url)
 {
 $result['page_url'] = $host . $url;
 }
return $result;
}
/**
 * 最终调用函数
 * @param string $keywords 搜索关键字
 * @return array
 */
function getData($keywords)
{
 $hrefList = getListHrefAll($keywords);
 $result = array();
 foreach ($hrefList as $k=>$v)
 {
 $result[] = getDetail($v);
 }
 return $result;
}
// Test Script
$keywords = trim($_GET['keywords']);
$result = getData($keywords);
print_r($result);
PHP 相关文章推荐
实现php加速的eAccelerator dll支持文件打包下载
Sep 30 PHP
php 远程关机操作的代码
Dec 05 PHP
解决ajax+php中文乱码的方法详解
Jun 09 PHP
PHP 冒泡排序 二分查找 顺序查找 二维数组排序算法函数的详解
Jun 25 PHP
PHP Curl出现403错误的解决办法
May 29 PHP
使用array_map简单搞定PHP删除文件、删除目录
Oct 29 PHP
Yii实现多按钮保存与提交的方法
Dec 03 PHP
PHP将session信息存储到数据库的类实例
Mar 04 PHP
PHP的pcntl多进程用法实例
Mar 19 PHP
深入解析PHP的Yii框架中的event事件机制
Mar 17 PHP
Yii2.0 模态弹出框+ajax提交表单
May 22 PHP
详解PHP序列化和反序列化原理
Jan 15 PHP
php内核解析:PHP中的哈希表
Jan 30 #PHP
php缓冲 output_buffering和ob_start使用介绍
Jan 30 #PHP
PHP内核探索:变量概述
Jan 30 #PHP
PHP内核探索:变量存储与类型使用说明
Jan 30 #PHP
PHP $_FILES中error返回值详解
Jan 30 #PHP
带密匙的php加密解密示例分享
Jan 29 #PHP
PHP过滤★等特殊符号的正则
Jan 27 #PHP
You might like
基于PHP实现假装商品限时抢购繁忙的效果
2015/10/16 PHP
PHP使用xpath解析XML的方法详解
2017/05/20 PHP
PHP单例模式与工厂模式详解
2017/08/29 PHP
firefox中JS读取XML文件
2006/12/21 Javascript
javascript操作文本框readOnly
2007/05/15 Javascript
IE不支持getElementsByClassName最终完美解决方案
2012/12/17 Javascript
JavaScript获取/更改文本框的值的实例代码
2013/08/02 Javascript
angular简介和其特点介绍
2015/01/29 Javascript
JavaScript实现节点的删除与序号重建实例
2015/08/05 Javascript
jQuery图片旋转插件jQueryRotate.js用法实例(附demo下载)
2016/01/21 Javascript
Bootstrap框架的学习教程详解(二)
2016/10/18 Javascript
jQuery Ajax实现跨域请求
2017/01/21 Javascript
Javascript中JSON数据分组优化实践及JS操作JSON总结
2017/12/22 Javascript
9种使用Chrome Firefox 自带调试工具调试javascript技巧
2017/12/22 Javascript
JS Generator 函数的含义与用法实例总结
2020/04/08 Javascript
JavaScript实现捕获鼠标坐标
2020/04/12 Javascript
Python遍历zip文件输出名称时出现乱码问题的解决方法
2015/04/08 Python
在Mac OS上搭建Python的开发环境
2015/12/24 Python
python虚拟环境virtualenv的使用教程
2017/10/20 Python
Python2.7实现多进程下开发多线程示例
2019/05/31 Python
PyQt5基本控件使用之消息弹出、用户输入、文件对话框的使用方法
2019/08/06 Python
python 多进程队列数据处理详解
2019/12/23 Python
Python pandas库中的isnull()详解
2019/12/26 Python
Python 实现判断图片格式并转换,将转换的图像存到生成的文件夹中
2020/01/13 Python
利用setuptools打包python程序的方法步骤
2020/01/18 Python
浅析两列自适应布局的3种思路
2016/05/03 HTML / CSS
出纳岗位职责范本
2013/12/01 职场文书
关于幼儿的自我评价
2013/12/18 职场文书
大学生实习鉴定评语
2014/04/25 职场文书
2014五年级班主任工作总结
2014/12/05 职场文书
五年级作文之劳动作文
2019/11/12 职场文书
python 逐步回归算法
2021/04/06 Python
python 算法题——快乐数的多种解法
2021/05/27 Python
解决mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO/YES)
2021/06/26 MySQL
Mysql如何实现不存在则插入,存在则更新
2022/03/25 MySQL
Redis全局ID生成器的实现
2022/06/05 Redis