分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)_php实例

复制代码 代码如下:

<?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); //是否抓取跳转后的页面
 $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 = 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;
}

/**
 * 获取列表页的html源码
 * @param string $keywords 搜索关键字
 * @param int $page 页数
 * @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提交数据
 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;
}

/**
 * 获取列表页 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 {
 // 没有匹配项
 return array();
 }
}

/**
 * 获取下一页数字
 * @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 搜索关键字
 * @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);
 while ($nextPage > 0)
 {
 $html = getListHtml($keywords, $nextPage);
 $tmpHrefList = getListHref($html);
 $hrefList = array_merge($hrefList, $tmpHrefList);
 $nextPage = getListNextPage($html);
 }
 return $hrefList;
}

/**
 * 获取详情页信息
 * @param string $url url地址或者是抓取到的html源代码 根据@see $is_url 区分
 * @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' => '', // 供应商型号
 '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 = '/<td>产品型号<\/td><td>(.[^<]+)</isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['mfg_part'] = trim($matches[1]);
 } else {
 // 此项木有,说明也没处处了
 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>详细资料<\/td><td><a\s+href="(.[^"]+)"/isU';
 if (preg_match($pattern, $html, $matches))
 {
 $result['pdf_url'] = trim($matches[1]);
 }

 // sup_stock
 $pattern = '/<td>库存数量<\/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;
}

/**
 * 最终调用函数
 * @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);

时间: 2024-09-02 19:28:39

分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)_php实例的相关文章

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

复制代码 代码如下: <?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)) { $da

分享PHP源码批量抓取远程网页图片并保存到本地的实现方法_php实例

做为一个仿站工作者,当遇到网站有版权时甚至加密的时候,WEBZIP也熄火,怎么扣取网页上的图片和背景图片呢.有时候,可能会想到用火狐,这款浏览器好像一个强大的BUG,文章有版权,屏蔽右键,火狐丝毫也不会被影响. 但是作为一个热爱php的开发者来说,更多的是喜欢自己动手.所以,我就写出了下面的一个源码,php远程抓取图片小程序.可以读取css文件并抓取css代码中的背景图片,下面这段代码也是针对抓取css中图片而编写的. <?php header("Content-Type: text/ht

PHP抓取、分析国内视频网站的视频信息工具类_php实例

使用方法: 复制代码 代码如下: require_once "VideoUrlParser.class.php";$url = "http://v.youku.com/v_show/id_XMjkwMzc0Njg4.html";$info = VedioUrlParser::parse($url);echo $info; 说明:调用该工具php文件VideoUrlParser.class.php,$url变量后面的字符串为视频页的地址,然后使用echo输出变量$in

php下通过curl抓取yahoo boss 搜索结果的实现代码

1.编写curl类,进行网页内容抓取 复制代码 代码如下: class CurlUtil { private $curl; private $timeout = 10; /** * 初始化curl对象 */ public function __construct() { $this->curl = curl_init(); curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->curl, CURLO

php 模拟手机访问页面并抓取数据

问题描述 php 模拟手机访问页面并抓取数据 $ch = curl_init(); $timeout = 0; curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 l

[20140212]linux下使用tcpdump抓取sql语句

[20140212]linu下使用tcpdump抓取sql语句.txt 我们生产系统问题多多,经常要跟踪用户执行的sql语句,当出现问题时要跟踪比较麻烦,我需要一个快捷的方式"看到"用户执行的sql语 句,想到了tcpdump抓包软件. 我测试建立shell脚本如下: #! /bin/bash /usr/sbin/tcpdump -l -i eth0 -s 16384 -A -nn src host $1 and dst port 1521 --说明: -- -l     Make s

javascript实时抓取天气和汇率的代码

问题描述 javascript实时抓取天气和汇率的代码 前台直接通过接口抓取,网上的说明看不懂啊,有没有完整代码好好研究一下,谢谢了

asp 抓取baidu收录网页数代码

asp 抓取baidu收录网页数代码 <% response.expires = -1 response.addheader "cache-control","no-cache" Response.AddHeader "Pragma","no-cache" wd=Request("d") If Request("s")="baidus" Then BaiduUr

页面数据抓取-关于网页表格抓取问题,用什么软件

问题描述 关于网页表格抓取问题,用什么软件 我要抓取的是一个系统内部网页,不能外部访问,数据表格的链接插入有插件.怎么能实现动态抓取??大神啊 解决方案 用抓包的方法最好,然后再分析数据,如果不行,可以考虑用webbrowse这个控件,里面的内容也比较好抓取