分享下页面关键字抓取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公用函数列表[正则]
Feb 22 PHP
PHP连接SQLServer2005 的问题解决方法
Jul 19 PHP
php自动获取字符串编码函数mb_detect_encoding
May 31 PHP
解析php安全性问题中的:Null 字符问题
Jun 21 PHP
浅析php学习的路线图
Jul 10 PHP
详解php比较操作符的安全问题
Dec 03 PHP
Linux安装配置php环境的方法
Jan 14 PHP
解决yii2左侧菜单子级无法高亮问题的方法
May 08 PHP
php英文单词统计器
Jun 23 PHP
针对多用户实现头像上传功能PHP代码 适用于登陆页面制作
Aug 17 PHP
PHP编写简单的App接口
Aug 28 PHP
php技巧小结【推荐】
Jan 19 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 ajax 静态分页过程形式
2011/09/02 PHP
PHP实现登陆表单提交CSRF及验证码
2017/01/24 PHP
php使用自带dom扩展进行元素匹配的原理解析
2020/05/29 PHP
js禁止回车提交表单的示例代码
2013/12/23 Javascript
用js替换除数字与逗号以外的所有字符的代码
2014/06/07 Javascript
JavaScript SweetAlert插件实现超酷消息警告框
2016/01/28 Javascript
Bootstrap Chart组件使用教程
2016/04/28 Javascript
原生js编写焦点图效果
2016/12/08 Javascript
微信小程序 传值取值的几种方法总结
2017/01/16 Javascript
微信小程序开发之toast提示插件使用示例
2017/06/08 Javascript
JavaScript函数式编程(Functional Programming)纯函数用法分析
2019/05/22 Javascript
JS实现给数组对象排序的方法分析
2019/06/24 Javascript
对layer弹出框中icon数字参数的说明介绍
2019/09/04 Javascript
layui prompt 设置允许空白提交的方法
2019/09/24 Javascript
React学习之受控组件与数据共享实例分析
2020/01/06 Javascript
Vue实现购物车实例代码两则
2020/05/30 Javascript
使用Python编写vim插件的简单示例
2015/04/17 Python
Python下线程之间的共享和释放示例
2015/05/04 Python
浅谈Python的文件类型
2016/05/30 Python
Python数据结构之单链表详解
2017/09/12 Python
在Python中pandas.DataFrame重置索引名称的实例
2018/11/06 Python
python实现推箱子游戏
2020/03/25 Python
Python中的 enum 模块源码详析
2019/01/09 Python
python获取微信企业号打卡数据并生成windows计划任务
2019/04/30 Python
python之信息加密题目详解
2019/06/26 Python
python+opencv像素的加减和加权操作的实现
2019/07/14 Python
Python实现Word文档转换Markdown的示例
2020/12/22 Python
python中str内置函数用法总结
2020/12/27 Python
pytorch中index_select()的用法详解
2021/01/06 Python
详解Canvas 跨域脱坑实践
2018/11/07 HTML / CSS
美国礼品卡商城: Gift Card Mall
2017/08/25 全球购物
自荐信的两点禁忌
2013/10/30 职场文书
致百米运动员广播稿
2014/01/29 职场文书
群众路线查摆问题整改措施
2014/10/10 职场文书
2015年幼儿园班主任个人工作总结
2015/10/22 职场文书
网络研修随笔感言
2015/11/18 职场文书