在下面的页面中匹配出列表中的“商品名称”和“链接地址”,以及对应“价格链接”
http://www.newegg.com.cn/Search.aspx?keyword=ipad2

解决方案 »

  1.   


    #! /bin/bash
    #shell code
    wget http://www.newegg.com.cn/Search.aspx?keyword=ipad2 response.txt$s = file_get_contents('response.txt');
    $p = '{<div class="itemCell">.*?<div class="title">.*?<a href="([^"]+?)".*?>([^<]+?)</a>.*?</div>.*?</div>}s';
    preg_match_all($p, $s, $m);
    print_r($m);
      

  2.   


    #结果中的第1,2项为
        [1] => Array
            (
                [0] => http://www.newegg.com.cn/Product/A36-184-7G8.htm
                [1] => http://www.newegg.com.cn/Product/A36-184-7GC.htm
                [2] => http://www.newegg.com.cn/Product/A60-0XF-037.htm
                [3] => http://www.newegg.com.cn/Product/A60-0XF-035.htm
                [4] => http://www.newegg.com.cn/Product/A60-0XF-05H-03.htm
                [5] => http://www.newegg.com.cn/Product/A60-0XF-05G-05.htm
                [6] => http://www.newegg.com.cn/Product/A60-0XF-05H-04.htm
                [7] => http://www.newegg.com.cn/Product/A60-0XF-05G-04.htm
                [8] => http://www.newegg.com.cn/Product/A60-0XF-034.htm
                [9] => http://www.newegg.com.cn/Product/A60-0XF-05H-02.htm
                [10] => http://www.newegg.com.cn/Product/A60-0XF-038.htm
                [11] => http://www.newegg.com.cn/Product/A60-0XF-05H-05.htm
                [12] => http://www.newegg.com.cn/Product/A60-0XF-05H.htm
                [13] => http://www.newegg.com.cn/Product/A60-0XF-05J.htm
                [14] => http://www.newegg.com.cn/Product/A60-0XF-05F.htm
                [15] => http://www.newegg.com.cn/Product/A60-0XF-036.htm
                [16] => http://www.newegg.com.cn/Product/A60-0XF-05G-02.htm
                [17] => http://www.newegg.com.cn/Product/A60-0XF-05J-02.htm
                [18] => http://www.newegg.com.cn/Product/A60-0XF-033.htm
                [19] => http://www.newegg.com.cn/Product/A60-0XF-05G-03.htm
                [20] => http://www.newegg.com.cn/Product/A60-0XF-05J-03.htm
                [21] => http://www.newegg.com.cn/Product/A60-0XF-032.htm
                [22] => http://www.newegg.com.cn/Product/A60-0LA-04T.htm
                [23] => http://www.newegg.com.cn/Product/A60-184-047.htm
            )    [2] => Array
            (
                [0] => Apple 苹果 iPad2 平板电脑 MC916CH/A 64G WIFI版 黑色
                [1] => Apple 苹果 iPad2 平板电脑 MC981CH/A 64G WIFI版 白色
                [2] => TUNEWEAR 腾威尔 IPAD2-TUN-FILM-01保护膜 高透
                [3] => TUNEWEAR 腾威尔 IPAD2-SOFT-SHELL-02 柔软硅胶保护套 粉红 &#40;含膜&#41;
                [4] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR03 尼龙单肩挎包 红色&#40;含膜&#41;
                [5] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-05 PU皮包 驼色配棕 &#40;含膜&#41;
                [6] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR04 尼龙单肩挎包 蓝色&#40;含膜&#41;
                [7] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-04 PU皮包 棕配黑 &#40;含膜&#41;
                [8] => TUNEWEAR 腾威尔 IPAD2-SOFT-SHELL-01 柔软硅胶保护套 浅灰 &#40;含膜&#41;
                [9] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR02 尼龙单肩挎包 棕色&#40;含膜&#41;
                [10] => TUNEWEAR 腾威尔 IPAD2-TUN-FILM-02 屏幕保护膜 防眩目 磨砂
                [11] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR05 尼龙单肩挎包 肉色&#40;含膜&#41;
                [12] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-UR01 尼龙单肩挎包 黑色&#40;含膜&#41;
                [13] => Tunewear 腾威尔 IPAD2-EGG-SHELL-SC03 超薄硬保护胶套 &#40;带smartcover接口&#41;白色
                [14] => Tunewear 腾威尔 IPAD2-CARBON-01 仿碳纤硬保护套 黑色 &#40;含膜&#41;
                [15] => TUNEWEAR 腾威尔 IPAD2-SOFT-SHELL-03 柔软硅胶保护套 蓝色 &#40;含膜&#41;
                [16] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 &#40;含膜&#41;
                [17] => Tunewear 腾威尔 IPAD2-EGG-SHELL-SC02 超薄硬保护胶套 &#40;带smartcover接口&#41;浅灰
                [18] => TUNEWEAR 腾威尔 IPAD2-EGG-SHELL-01 蛋壳质感超薄保护壳 透明 &#40;含膜&#41;
                [19] => Tunewear 腾威尔 IPAD2-TUN-FOLIO-03 PU皮包 橙配黑 &#40;含膜&#41;
                [20] => Tunewear 腾威尔 IPAD2-EGG-SHELL-SC01 超薄硬保护胶套 &#40;带smartcover接口&#41;透明
                [21] => TUNEWEAR 腾威尔 IPAD2-EGG-SHELL-02 蛋壳质感超薄保护壳 浅灰 &#40;含膜&#41;
                [22] => i-kodoo 爱酷多 ipad2 smart cover 智能四折立式 PU套 CPI-20 灰色
                [23] => Apple 苹果 iPad2 原装保护套 Smart Cover MC945FE/A 聚氨酯 橙色
            )
      

  3.   

    这个可以用DOM解析,比如说getElementsByTagname、现在js里也支持getelementByClass了,这样解析那个HTML就更简单了,具体你再查查资料、、这只是思路。当然你也可以用正则,<div class="itemCell"><dl class="inner"><dt>
    <p class="brand"></p><p class="iconPromB"><span>
    <img src="http://c2.neweggimages.com.cn/WebResources/2009/Default/Nest/img/promIcon/promIcon04.gif"/>
    </span></p>
     
     
    <a href="http://www.newegg.com.cn/Product/A60-0XF-05G-02.htm"><img src="http://c1.neweggimages.com.cn/neweggpic2/P144/A60/0XF/A60-0XF-05G-02.jpg" title="Tunewear&#32;腾威尔&#32;IPAD2-TUN-FOLIO-01&#32;PU皮包&#32;黑配棕&#32;&#40;含膜&#41;" /></a></dt>
    <dd class="pdInfo"><div class="title">
    <p class="info"><a href="http://www.newegg.com.cn/Product/A60-0XF-05G-02.htm" title="Tunewear&#32;腾威尔&#32;IPAD2-TUN-FOLIO-01&#32;PU皮包&#32;黑配棕&#32;&#40;含膜&#41;">Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 &#40;含膜&#41;</a></p>
     
    <p class="prom null"></p>
     
    <p class="extraIcon null"></p>
    </div>
    <ul class="bullet"><LI>TUNEWEAR 腾威尔<LI>笔记簿形保护套<LI>IPAD2-TUN-FOLIO-05</ul></dd>
    <dd class="price"><p class="bypast"><span>¥309.00</span></p><p class="current"><strong class="price"><span>¥</span>258.00</strong></p>
     
     
    </dd>
    <dd class='rank '></dd>
    <dd class="btnArea"><a href="http://www.newegg.com.cn/Shopping/ShoppingCart.aspx?action=Add&productno=165766" class="cmnbtn btnG"><span>购买</span></a><a href="https://secure.newegg.com.cn/Customer/WishList.aspx?action=Add&productno=165766" class="cmnbtn btnH"><span>收藏</span></a></dd></dl>
    </div>
     如上面的代码:这是链接地址:<p class="info"><a href="http://www.newegg.com.cn/Product/A60-0XF-05G-02.htm" title="Tunewear&#32;腾威尔&#32;IPAD2-TUN-FOLIO-01&#32;PU皮包&#32;黑配棕&#32;&#40;含膜&#41;">Tunewear 腾威尔 IPAD2-TUN-FOLIO-01 PU皮包 黑配棕 &#40;含膜&#41;</a></p>这是商品名称和价格:
    <ul class="bullet"><LI>TUNEWEAR 腾威尔<LI>笔记簿形保护套<LI>IPAD2-TUN-FOLIO-05</ul></dd>
    <dd class="price"><p class="bypast"><span>¥309.00</span></p><p class="current"><strong class="price"><span>¥</span>258.00</strong></p>以价格为例:
    <dd class="price"><p class="bypast"><span>¥309.00</span></p><p class="current"><strong class="price"><span>¥</span>258.00</strong></p>
    可以这样写,只需做些代换:
    <dd class="price"><p class="bypast"><span>¥<!--这里换成浮点数的正则,如 \s ^(-?\d+)(\.\d+)? \s  --></span></p><p class="current"><strong class="price"><span>¥</span> \s ^(-?\d+)(\.\d+)? \s</strong></p>如果上面的不行,将里面的< /> 等特殊符号,转义一下,或用实体表示试下,如果不对,请指教