php - 使用 PHP 从特定的 div id 获取数据

标签 php html fetch

<分区>

Possible Duplicate:
read XML tag id from php

使用 PHP 从特定的 div id 获取数据的方法是什么。我想要做的是从一个名为 <div id="content"> 的 div id 中获取数据,因此来自该 div id 的所有数据都将在一个变量中获取。
我可以使用我的脚本获取所有内容,但无法过滤它以从特定的 div 标签获取数据。
这是我的脚本我正在使用获取任何内容:

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";


有什么想法吗?

最佳答案

试试这个,但要确保你已经下载并包含了 PHP Simple HTML DOM Parser

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

他们有一些方法可以从 div id 或 Tag id 中排除内容,例如,

1) Using regex

2) Using SimpleXML

3) 与 DOM extension或者 XPath

关于php - 使用 PHP 从特定的 div id 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12246636/

相关文章:

php - 未定义索引存在 POST 问题

php - 从mysql背景理解mongodb结构

php - 如果数据为空,特定的加密算法如何工作

javascript - 来自 API 的变量未传递到 Angular 中的本地存储

reactjs - Continuously Fetch API 对于新数据 React Fetch

php - lastinsertId() 与 exec() 一起使用时不能与execute() 一起使用?

javascript - 永远是平局!剪刀石头布游戏

html - 菜单大小取决于屏幕分辨率

jquery - 从百分比到初始的 CSS 过渡

java - 使用 JGit 获取不显示最后一次提交