php 抓取div内容

1. 取得指定網頁內的所有圖片測試

  1. <?php   
  2. //取得指定位址的內容,並儲存至text   
  3. $text=file_get_contents('http://andy.diimii.com/');    
  4.   
  5. //取得所有img標籤,並儲存至二維陣列match   
  6. preg_match_all('#<img[^>]*>#i'$text$match);   
  7.   
  8. //印出match   
  9. print_r($match);   
  10. ?>  
<?php
//取得指定位址的內容,並儲存至text
$text=file_get_contents('http://andy.diimii.com/'); 

//取得所有img標籤,並儲存至二維陣列match
preg_match_all('#<img[^>]*>#i', $text, $match);

//印出match
print_r($match);
?>

2. 取得指定網頁內的第一張圖片測試

  1. <?php   
  2. //取得指定位址的內容,並儲存至text   
  3. $text=file_get_contents('http://andy.diimii.com/');   
  4.   
  5. //取得第一個img標籤,並儲存至陣列match(regex語法與上述同義)   
  6. preg_match('/<img[^>]*>/Ui'$text$match);   
  7.   
  8. //印出match   
  9. print_r($match);   
  10. ?>  
<?php
//取得指定位址的內容,並儲存至text
$text=file_get_contents('http://andy.diimii.com/');

//取得第一個img標籤,並儲存至陣列match(regex語法與上述同義)
preg_match('/<img[^>]*>/Ui', $text, $match);

//印出match
print_r($match);
?>

3. 取得指定網頁內的特定div區塊(藉由id判斷)測試

  1. <?php   
  2. //取得指定位址的內容,並儲存至text   
  3. $text=file_get_contents('http://andy.diimii.com/2009/01/seo%e5%8c%96%e7%9a%84%e9%97%9c%e9%8d%b5%e5%ad%97%e5%bb%a3%e5%91%8a%e9%80%a3%e7%b5%90/');    
  4.   
  5. //去除換行及空白字元(序列化內容才需使用)   
  6. //$text=str_replace(array("/r","/n","/t","/s"), '', $text);      
  7.   
  8. //取出div標籤且id為PostContent的內容,並儲存至陣列match   
  9. preg_match('/<div[^>]*id="PostContent"[^>]*>(.*?) <//div>/si',$text,$match);   
  10.   
  11. //印出match[0]   
  12. print($match[0]);   
  13. ?>  
<?php
//取得指定位址的內容,並儲存至text
$text=file_get_contents('http://andy.diimii.com/2009/01/seo%e5%8c%96%e7%9a%84%e9%97%9c%e9%8d%b5%e5%ad%97%e5%bb%a3%e5%91%8a%e9%80%a3%e7%b5%90/'); 

//去除換行及空白字元(序列化內容才需使用)
//$text=str_replace(array("/r","/n","/t","/s"), '', $text);   

//取出div標籤且id為PostContent的內容,並儲存至陣列match
preg_match('/<div[^>]*id="PostContent"[^>]*>(.*?) <//div>/si',$text,$match);

//印出match[0]
print($match[0]);
?>

4. 上述2及3的結合測試

  1. <?php   
  2. //取得指定位址的內容,並儲存至text   
  3. $text=file_get_contents('http://andy.diimii.com/2009/01/seo%e5%8c%96%e7%9a%84%e9%97%9c%e9%8d%b5%e5%ad%97%e5%bb%a3%e5%91%8a%e9%80%a3%e7%b5%90/');       
  4.   
  5. //取出div標籤且id為PostContent的內容,並儲存至陣列match   
  6. preg_match('/<div[^>]*id="PostContent"[^>]*>(.*?) <//div>/si',$text,$match);      
  7.   
  8. //取得第一個img標籤,並儲存至陣列match2   
  9. preg_match('/<img[^>]*>/Ui'$match[0], $match2);    
  10.   
  11. //印出match2[0]   
  12. print_r($match2[0]);   
  13. ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值