2009-07-17 74 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>"; 
}