分享下页面关键字抓取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 缓冲的免费实现方法
Oct 09 PHP
深入理解PHP之数组(遍历顺序)  Laruence原创
Jun 13 PHP
php 对输入信息的进行安全过滤的函数代码
Jun 29 PHP
php 获取页面中指定内容的实现类
Jan 23 PHP
PHP mkdir()无写权限的问题解决方法
Jun 19 PHP
php中error与exception的区别及应用
Jul 28 PHP
CodeIgniter视图使用注意事项
Jan 20 PHP
php array_keys 返回数组的键名
Oct 25 PHP
php实现文件预览功能
May 23 PHP
关于Curl在Swoole协程中的解决方案详析
Sep 12 PHP
PHP切割整数工具类似微信红包金额分配的思路详解
Sep 18 PHP
Laravel框架实现抢红包功能示例
Oct 31 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
生成sessionid和随机密码的例子
2006/10/09 PHP
php实现设计模式中的单例模式详解
2014/10/11 PHP
php删除数组中重复元素的方法
2015/12/22 PHP
php模仿qq空间或朋友圈发布动态、评论动态、回复评论、删除动态或评论的功能(中)
2017/06/11 PHP
wordpress自定义标签云与随机获取标签的方法详解
2019/03/22 PHP
Linux下安装Memcached服务器和客户端与PHP使用示例
2019/04/15 PHP
alixixi runcode.asp的代码不错的应用
2007/08/08 Javascript
通用javascript脚本函数库 方便开发
2009/10/13 Javascript
juqery 学习之五 文档处理 插入
2011/02/11 Javascript
Mac/Windows下如何安装Node.js
2013/11/22 Javascript
jquery复选框全选/取消示例
2013/12/30 Javascript
使用typeof方法判断undefined类型
2014/09/09 Javascript
angular源码学习第一篇 setupModuleLoader方法
2016/10/20 Javascript
jQuery实现花式轮播之圣诞节礼物传送效果
2016/12/25 Javascript
AngularJS标签页tab选项卡切换功能经典实例详解
2018/05/16 Javascript
elementUI中Table表格问题的解决方法
2018/12/04 Javascript
[02:46]解说DC:感谢430陪伴我们的DOTA2国际邀请赛岁月
2016/06/29 DOTA
[02:47]2018年度DOTA2最佳辅助位选手4号位-完美盛典
2018/12/17 DOTA
Python 文件操作实现代码
2009/10/07 Python
Python 多进程和数据传递的理解
2017/10/09 Python
Python面向对象程序设计类的多态用法详解
2019/04/12 Python
PyQT5 QTableView显示绑定数据的实例详解
2019/06/25 Python
Python StringIO如何在内存中读写str
2020/01/07 Python
Python连接Oracle之环境配置、实例代码及报错解决方法详解
2020/02/11 Python
在PyCharm中实现添加快捷模块
2020/02/12 Python
python爬虫学习笔记之pyquery模块基本用法详解
2020/04/09 Python
详解使用postMessage解决iframe跨域通信问题
2019/11/01 HTML / CSS
三八节主持词
2014/03/17 职场文书
电气工程及其自动化专业毕业生自荐信
2014/06/21 职场文书
品牌转让协议书
2014/08/20 职场文书
民政局个人整改措施
2014/09/24 职场文书
党员三严三实对照检查材料
2014/10/13 职场文书
卡特教练观后感
2015/06/08 职场文书
企业安全生产检查制度
2015/08/06 职场文书
有关保护环境的宣传标语100条
2019/08/07 职场文书
pandas中对文本类型数据的处理小结
2021/11/01 Python