分享下页面关键字抓取components.arrow.com站点代码
程序员文章站
2024-01-12 19:00:40
复制代码 代码如下:
<?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
/**
* 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);
推荐阅读
-
分享下页面关键字抓取components.arrow.com站点代码
-
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)_PHP
-
分享下页面关键字抓取components.arrow.com站点代码_PHP教程
-
分享下页面关键字抓取components.arrow.com站点代码
-
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)
-
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)
-
分享下页面关键字抓取components.arrow.com站点代码_php实例
-
分享下页面关键字抓取components.arrow.com站点代码
-
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)
-
分享下页面关键字抓取components.arrow.com站点代码