Fetching Data From A Specific div id Using PHP [duplicate]

狂风中的少年 提交于 2019-12-12 16:12:22

问题


Possible Duplicate:
read XML tag id from php

what is the way to fetch data from A Specific div id Using PHP. What i want to do is to fetch data from a div id called <div id="content"> , so all the data from that div id will be fetched in a variable.
I can fetch all content with my script but cant filter it to fetch data from a Specific div tag.
Here is the script i am using to fetch any content:

function file_get_contents_curl($url)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}
$html = file_get_contents_curl("http://www.example.com");
//parsing all content:
$doc = new DOMDocument();
@$doc->loadHTML($html);
echo "$html";


any idea?


回答1:


Try this but make sure you have downloaded and included PHP Simple HTML DOM Parser

$html = file_get_html("http://www.example.com");
$displaybody = $html->find('div[id=content]', 0)->plaintext;

Their are some ways to exclude content from div id or Tag id like,

1) Using regex

2) Using SimpleXML

3) With DOM extension Or XPath




回答2:


With Simple HTML DOM Parser

<?php

$content = file_get_contents_curl("http://www.example.com");

$html = file_get_html($content );
$ret = $html->find('div[id=divname]');
?>


来源:https://stackoverflow.com/questions/12246636/fetching-data-from-a-specific-div-id-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!