分享下页面关键字抓取components.arrow.com站点代码


Posted in PHP onJanuary 30, 2014
<?php
 /**
 * HOST: components.arrow.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 = 15, $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 $start 开始记录数
 * @return boolean|array
 */
 function getListHtml($keywords, $start = 0)
 {
 if ($start < 0)
 {
 return false;
 }
$postData = array(
 'search_token' => $keywords,
 'start' => $start,
 'limit' => 100,
 );
$result = curl_post('http://components.arrow.com/part/search/' . $keywords, http_build_query($postData));
 if ( isset($result['error']) )
 {
 return false;
 //exit($result['error']);
 }
 $result = $result['result'];
return $result;
 }
/**
 * 获取列表页 连接href
 * @param string $html html源码
 * @return array
 */
 function getListHref($html)
 {
 $pattern = '/<td\s+class="col_mfr_part_num"><a\s+href="(.[^>]+)">/isU';
 if (preg_match_all($pattern, $html, $matches))
 {
 return $matches[1];
 } else {
 // 没有匹配项
 return array();
 }
 }
/**
 * 获取下一页数字start
 * @param string $html html源码
 * @return number
 */
 function getListNextPage($html)
 {
 $pattern = '/<script\s+language="javascript">buildPagination\(\'\d+\',\'\d+\',\'(\d+)\',\d+\);<\/script>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 return intval($matches[1]);
 } else {
 return -1;
 }
 }
/**
 * 获取列表也所有的详细列表
 * @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);
 $loop =0;
 while ($nextPage > 0)
 {
 $html = getListHtml($keywords, $nextPage);
 $tmpHrefList = getListHref($html);
 $hrefList = array_merge($hrefList, $tmpHrefList);
 $nextPage = getListNextPage($html);
 $loop ++;
 }
 return $hrefList;
 }
/**
 * 获取详情页信息
 * @param string $url url地址
 * @return array()
 */
 function getDetail($url)
 {
 if ( empty($url) )
 {
 return false;
 }
 $host = 'http://components.arrow.com';
$url = $host . $url;
 $result = curl_get($url);
 if ( isset($result['error']) )
 {
 return array();
 //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 = '/<li>[\s\n]*<strong>Part No:\s*<\/strong>(.+)<\/li>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['mfg_part'] = trim($matches[1]);
 } else {file_put_contents('page.txt', $html);die('xxx');
 return array();
 }
// mfg_name
 $pattern = '/<li>[\s\n]*<strong>Manufacturer: <\/strong>(.+)<\/li>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['mfg_name'] = trim($matches[1]);
 }
// cat_name
 $pattern = '/displayCategory\(\'(.[^\']+)\'\);/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['cat_name'] = trim($matches[1]);
 $result['cat_name'] = str_replace('|', '>', $result['cat_name']);
 }
// para
 $tablepattern = '/<table\s+id="part_specs".[^>]*>(.+)<\/table>/isU';
 if (preg_match($tablepattern, $html, $matches))
 {
 $pattern = '/<tr>[\s\n]*<td><strong>(.+)<\/strong><\/td><td>(.+)<\/td>[\s\n]*<\/tr>/isU';
 if (preg_match_all($pattern, $matches[1], $matches))
 {
 foreach($matches[1] as $k=>$v)
 {
 $v = trim($v);
 if ('Package Type' == $v)
 {
 $result['package'] = trim($matches[2][$k]);
 continue;
 }
 $result['para'][$v] = trim($matches[2][$k]);
 }
 }
 }
// desc
 $pattern = '/<div\s+id="part_title">.+<h4>(.+)<\/h4>[\s\n]*<\/div>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['desc'] = trim($matches[1]);
 }
// pdf_url
 $pattern = '/<li\s+class="datasheet">[\s\n]*<strong>Datasheet:<\/strong><a\s+href="(.[^"]+)"/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['pdf_url'] = $host . trim($matches[1]);
 }
// sup_stock
 $pattern = '/<td\s+id="inv_1"\s+class="li_inv">([\d,]+)<\/td>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['sup_stock'] = trim($matches[1]);
 $result['sup_stock'] = str_replace(',', '', $result['sup_stock']);
 }
// min_purch
 $pattern = '/<span\s+id="multiples">[\s\n]*<strong>Multiple:\s*<\/strong>(.+)<\/span>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['min_purch'] = trim($matches[1]);
 }
// price
 $pattern = '/<div\s+id="price_1"\s+class="li_price">(.[^<]+)<\/div>/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['price'][1] = trim($matches[1]);
 }
 $pattern = '/<div\s+id="price_1"\s+class="li_price">[\s\n]*<span.[^>]+title="(.[^"]+)">/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $priceurl = str_replace('&', '&', $matches[1]);
 $json = curl_get($priceurl);
 $json = $json['result'];
 if (! empty($json))
 {
 $jsonresult = json_decode($json, true);
 foreach ($jsonresult['parts'][0]['webprice']['resale'] as $k=>$v)
 {
 $result['price'][$v['minqty']] = $v['price'];
 }
 }
 }
// img_url
 $pattern = '/<div\s+id="part_image">[\s\n]*<img\s+src="(.[^"]+)"/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['img_url'] = trim($matches[1]);
 }
// page_url
 $result['page_url'] = $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函数memory_get_usage获取PHP内存清耗量的方法
Dec 06 PHP
phpexcel导出excel的颜色和网页中的颜色显示不一致
Dec 11 PHP
三种php连接access数据库方法
Nov 11 PHP
PHP把空格、换行符、中文逗号等替换成英文逗号的正则表达式
May 04 PHP
php基于session实现数据库交互的类实例
Aug 03 PHP
Yii实现的多级联动下拉菜单
Jul 13 PHP
php获取数据库中数据的实现方法
Jun 01 PHP
PHP实现Redis单据锁以及防止并发重复写入
Apr 10 PHP
Yii框架中用response保存cookie,用request读取cookie的原理解析
Sep 04 PHP
laravel框架语言包拓展实现方法分析
Nov 22 PHP
php的单例模式及应用场景详解
Feb 27 PHP
你真的了解PHP中的引用符号(&)吗
May 12 PHP
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)
Jan 30 #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
You might like
PHP iconv 解决utf-8和gb2312编码转换问题
2010/04/12 PHP
CURL的学习和应用(附多线程实现)
2013/06/03 PHP
php实现session自定义会话处理器的方法
2015/01/27 PHP
PHP简单处理表单输入的特殊字符的方法
2016/02/03 PHP
js数字输入框(包括最大值最小值限制和四舍五入)
2009/11/24 Javascript
DWZ table的原生分页浅谈
2013/03/01 Javascript
使用js判断数组中是否包含某一元素(类似于php中的in_array())
2013/12/12 Javascript
使用javascript实现有效时间的控制,并显示将要过期的时间
2014/01/02 Javascript
nodejs后台集成ueditor富文本编辑器的实例
2017/07/11 NodeJs
vue文件运行的方法教学
2019/02/12 Javascript
微信小程序实现录音时的麦克风动画效果实例
2019/05/18 Javascript
微信小程序 WXML节点信息查询详解
2019/07/29 Javascript
python通过ElementTree操作XML获取结点读取属性美化XML
2013/12/02 Python
跟老齐学Python之有容乃大的list(1)
2014/09/14 Python
Python2.7读取PDF文件的方法示例
2017/07/13 Python
Python中函数eval和ast.literal_eval的区别详解
2017/08/10 Python
Python3 Random模块代码详解
2017/12/04 Python
python构建深度神经网络(DNN)
2018/03/10 Python
PyTorch上搭建简单神经网络实现回归和分类的示例
2018/04/28 Python
Anaconda下安装mysql-python的包实例
2018/06/11 Python
python 求一个列表中所有元素的乘积实例
2019/06/11 Python
python配置文件写入过程详解
2019/10/19 Python
Python关于__name__属性的含义和作用详解
2020/02/19 Python
jupyter notebook 实现matplotlib图动态刷新
2020/04/22 Python
基于Tensorflow读取MNIST数据集时网络超时的解决方式
2020/06/22 Python
Ubuntu16安装Python3.9的实现步骤
2020/12/15 Python
python实现scrapy爬虫每天定时抓取数据的示例代码
2021/01/27 Python
StudentUniverse英国:学生航班、酒店和旅游
2019/08/25 全球购物
KELLER SPORTS荷兰:在线订购最好的运动产品
2020/10/13 全球购物
资产评估专业学生的自我鉴定
2013/11/14 职场文书
毕业生求职信的经典写法
2014/01/31 职场文书
文明和谐家庭事迹材料
2014/05/18 职场文书
反四风对照检查材料
2014/09/22 职场文书
幼儿园小班教师随笔
2015/08/14 职场文书
2016年“12.4”法制宣传日活动总结
2016/04/01 职场文书
解决Mysql多行子查询的使用及空值问题
2022/01/22 MySQL