2009-07-17 16 views
1
頁面

內容我想檢索頁面的內容和其重新格式化爲我喜歡...PHP-檢索

例如:

  • 轉到example.com
  • 獲取內容與類標籤中的「x」
  • 通行證的內容與特定變量
  • 在一些非常form..array,CSV,XML吐出內容...

不太難,對吧?我是一個PHP noob! :)

回答

2

嘗試使用PHP Simple HTML DOM Parser

你可以做漂亮的東西是這樣的:

// Create DOM from URL or file 
$html = file_get_html('http://www.google.com/'); 

// Find all images 
foreach($html->find('img') as $element) 
     echo $element->src . '<br>'; 

// Find all links with class=x 
foreach($html->find('a[class=x]') as $element) 
     echo $element->href . '<br>'; 
0

XSD可能會爲你做的伎倆。我也會考慮wget + CSS ...

1

用於獲取數據,有三個難度級別:

file_get_contents($url); //easy 

不幸的是,很多網站都不是很響應合適的用戶代理。你有兩個選擇,在這裏。一個人比另一個人有點難。中間是Zend HTTP Client

$client = Zend_Http_Client(); //make sure to include Zend_Http, etc. 
$client->setConfig($params); // params will include proper user agent 
$client->setUri($aUrl); 
$html = $client->request()->getBody(); 

選擇三,你可能甚至要考慮,除非你真的想保持它更多的腳本不是面向對象的,是探索PHP的cURL functionality

有幾個PHP-通過DOM對象訪問HTML數據的本地方式,但我最喜歡的是Simple HTML DOM Parser。它非常類似於jQuery/CSS樣式的DOM導航。

$domObject = new Simple_HTML_Dom($html); 
foreach ($domobject->find('div#theDataYouWant p') as $sentence) 
{ 
    echo "<h3>{$sentence}</h3>"; 
}