使用PHP Curl获取特定的Div或Part

时间:2014-07-30 05:55:17

标签: php curl

<?php
$url='http://yoursite.com'; // link for the site
print_r(get_data($url)); //dumps the content, you can manipulate as you wish to

/* gets the data from a URL */

function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>

以上代码是获取整个站点数据,如何回显此代码的特定部分数据...帮助

2 个答案:

答案 0 :(得分:0)

以下是我使用的内容:Simple HTML DOM Parser

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

// Find all links 
foreach($html->find('a') as $element) 
       echo $element->href . '<br>';


// Find all article blocks
foreach($html->find('div.article') as $article) {
    $item['title']     = $article->find('div.title', 0)->plaintext;
    $item['intro']    = $article->find('div.intro', 0)->plaintext;
    $item['details'] = $article->find('div.details', 0)->plaintext;
    $articles[] = $item;
}

// Object Oriented Way
// Create a DOM object
$html = new simple_html_dom();

// Load HTML from a string
$html->load('<html><body>Hello!</body></html>');

// Load HTML from a URL 
$html->load_file('http://www.google.com/');

// Load HTML from a HTML file 
$html->load_file('test.htm');

答案 1 :(得分:0)

使用DOMDocument::loadHTML()DOMXPath::query()通过CURL请求从返回的HTML中获取所需的元素。

$dom = new DOMDocument();
$dom->loadHTML($data); // Returned $data from CURL request

$xpath = new DOMXPath($dom);
$elements = $xpath->query('*/div'); // your query here to filter out the elements you need

然后,您将获得$elements中加载的HTML的元素列表,这些元素与XPath查询匹配,您可以使用它们。

此方法的优点是您不需要第三方库,因为每个PHP&gt; = 5.0.0安装都会内置DOMDocumentDOMXPath

此外,XML Path Language (XPath)是W3C为解决XML / HTML元素而定义的标准。