分享下頁(yè)面關(guān)鍵字抓取www.icbase.com站點(diǎn)代碼(帶asp.net參數(shù)的)
更新時(shí)間:2014年01月30日 23:09:58 作者:
這篇文章主要介紹了分享下頁(yè)面關(guān)鍵字抓取www.icbase.com站點(diǎn)代碼(帶asp.net參數(shù)的)的相關(guān)資料,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
<?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); //是否抓取跳轉(zhuǎn)后的頁(yè)面
$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;
}
復(fù)制代碼 代碼如下:
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;
}
/**
* 獲取列表頁(yè)的html源碼
* @param string $keywords 搜索關(guān)鍵字
* @param int $page 頁(yè)數(shù)
* @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提交數(shù)據(jù)
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;
}
/**
* 獲取列表頁(yè) 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 {
// 沒有匹配項(xiàng)
return array();
}
}
/**
* 獲取下一頁(yè)數(shù)字
* @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 搜索關(guān)鍵字
* @return boolean|array
*/
function getListHrefAll($keywords)
{
if (empty($keywords))
{
return false;
}
$html = getListHtml($keywords);
$hrefList = getListHref($html);
if (empty($hrefList))
{
// 沒有結(jié)果
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;
}
/**
* 獲取詳情頁(yè)信息
* @param string $url url地址或者是抓取到的html源代碼 根據(jù)@see $is_url 區(qū)分
* @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' => '', // 供應(yīng)商型號(hào)
'sup_id' => '', // 供應(yīng)商ID
'mfg_part' => '', // 制造商型號(hào)
'mfg_name' => '', // 制造商名稱
'cat_name' => '', // 分類名稱
'para' => '', // 屬性
'desc' => '', // 描述
'pdf_url' => '', // PDF地址
'sup_stock' => '', // 庫(kù)存
'min_purch' => '', // 最小訂購(gòu)量
'price' => '', // 價(jià)格
'img_url' => '', // 圖片地址
'createtime' => '', // 創(chuàng)建時(shí)間
'datacode' => '', // 批號(hào)
'package' => '', // 封裝
'page_url' => '', // 頁(yè)面地址
);
// mfg_part
$pattern = '/<td>產(chǎn)品型號(hào)<\/td><td>(.[^<]+)</isU';
if (preg_match($pattern, $html, $matches))
{
$result['mfg_part'] = trim($matches[1]);
} else {
// 此項(xiàng)木有,說明也沒處處了
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>詳細(xì)資料<\/td><td><a\s+href="(.[^"]+)"/isU';
if (preg_match($pattern, $html, $matches))
{
$result['pdf_url'] = trim($matches[1]);
}
// sup_stock
$pattern = '/<td>庫(kù)存數(shù)量<\/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;
}
/**
* 最終調(diào)用函數(shù)
* @param string $keywords 搜索關(guān)鍵字
* @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);
您可能感興趣的文章:
- Asp.net給站點(diǎn)某目錄增加Aspnet用戶
- Asp.net中處理一個(gè)站點(diǎn)不同Web應(yīng)用共享Session的問題
- 充分利用ASP.NET的三種緩存提高站點(diǎn)性能的注意方法
- asp.net Web站點(diǎn)風(fēng)格切換的實(shí)現(xiàn)
- asp.net 站點(diǎn)URLRewrite使用小記
- asp.net 支持多語言站點(diǎn)的實(shí)現(xiàn)方法
- 導(dǎo)致Asp.Net站點(diǎn)重啟10個(gè)原因小結(jié)分析
- ASP.NET 站點(diǎn)地圖(sitemap)簡(jiǎn)明教程
- 設(shè)置ASP.NET頁(yè)面的運(yùn)行超時(shí)時(shí)間詳細(xì)到單個(gè)頁(yè)面及站點(diǎn)
- ASP.NET站點(diǎn)導(dǎo)航應(yīng)用詳解
- 如何建立ASP.NET站點(diǎn)
相關(guān)文章
laravel-admin利用ModelTree實(shí)現(xiàn)對(duì)分類信息的管理
這篇文章主要介紹了laravel-admin利用ModelTree實(shí)現(xiàn)對(duì)分類信息的管理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01PHP如何將數(shù)據(jù)庫(kù)查詢結(jié)果輸出為json格式
JSON數(shù)據(jù)格式相比xml的解析難度較低,配合Ajax技術(shù)的使用使得前端獲取數(shù)據(jù),呈現(xiàn)數(shù)據(jù)非常方便,所以下面這篇文章主要給大家介紹了關(guān)于PHP如何將數(shù)據(jù)庫(kù)查詢結(jié)果輸出為json格式的相關(guān)資料,需要的朋友可以參考下2022-02-02PHP程序級(jí)守護(hù)進(jìn)程的實(shí)現(xiàn)與優(yōu)化的使用概述
本篇文章介紹了,PHP程序級(jí)守護(hù)進(jìn)程的實(shí)現(xiàn)與優(yōu)化的使用概述。需要的朋友參考下2013-05-05PHP把JPEG圖片轉(zhuǎn)換成Progressive JPEG的方法
這篇文章主要介紹了PHP把JPEG圖片轉(zhuǎn)換成Progressive JPEG的方法,本文同時(shí)講解了Baseline JPEG和Progressive JPEG的區(qū)別和它們的優(yōu)劣,需要的朋友可以參考下2014-06-06thinkphp視圖模型查詢提示ERR: 1146:Table ''db.pr_order_view'' doesn''
這篇文章主要介紹了thinkphp視圖模型查詢提示ERR: 1146:Table 'db.pr_order_view' doesn't exist的解決方法,對(duì)于ThinkPHP初學(xué)者來說有一定的借鑒價(jià)值,需要的朋友可以參考下2014-10-10