zoukankan      html  css  js  c++  java
  • php抓取网页特定div区块及图片,从简单入手

    1. 取得指定网页內的所有图片:
    <?php
    //取得指定位址的內容,並储存至text
    $text=file_get_contents('http://yourweb/');
     
    //取得第一個img标签,並储存至阵列match(regex语法与上述同义)
    preg_match('/<img[^>]*>/Ui', $text, $match);
     
    //打印match
    print_r($match);
     
     
    -----------------
    2. 取得指定网页內的第一张图片:
    <?php
    //取得指定位址的內容,並储存至text
    $text=file_get_contents('http://yourweb/');
     
    //取得第一個img标签,並储存至阵列match(regex语法与上述同义)
    preg_match('/<img[^>]*>/Ui', $text, $match);
     
    //打印match
    print_r($match);
     
     
    ------------------------------------
     
     
    3. 取得指定网页內的特定div区块(藉由id判断):
    <?php
    //取得指定位址的內容,並储存至text
    $text=file_get_contents('http://yourweb/');
     
    //去除換行及空白字元(序列化內容才需使用)
    $text=str_replace(array(" "," "," ","s"), '', $text); 
     
    //取出div标签且id為PostContent的內容,並储存至阵列match
    preg_match('/<div[^>]*id="PostContent"[^>]*>(.*?) </div>/si',$text,$match);
     
    //打印match[0]
    print($match[0]);
     
     
    -------------------------------------------
    4. 上述2及3的结合:
    <?php
    //取得指定位址的內容,並储存至text
    $text=file_get_contents('http://yourweb/'); 
     
    //取出div标签且id為PostContent的內容,並储存至阵列match
    preg_match('/<div[^>]*id="PostContent"[^>]*>(.*?) </div>/si',$text,$match); 
     
    //取得第一個img标签,並储存至阵列match2
    preg_match('/<img[^>]*>/Ui', $match[0], $match2);
     
    //打印match2[0]
    print_r($match2[0]);
     
    转载请声明来源!
     
  • 相关阅读:
    假如
    Find the peace with yourself
    Sep 15th 2018
    Sep 10th 2018
    third party sales process 继续说
    成功设置open live writer
    sublime text2 基本配置及结合Python 环境
    Compmgmtlauncher.exe问题解决方法
    nginx 代理服务器
    vmware之linux不重启添加虚拟硬盘
  • 原文地址:https://www.cnblogs.com/ppeenngg/p/7198371.html
Copyright © 2011-2022 走看看