【发布时间】:2016-02-21 22:43:58
【问题描述】:
所以一个非常乐于助人的人帮助我在 Stackoverflow 上取得了这么大的进展,但是我需要将他的代码从 HTMl 转换为一个 URL 来抓取我一遍又一遍地尝试过并且我不断遇到错误的任何想法?
function getElementByIdAsString($html, $id, $pretty = true) {
$doc = new DOMDocument();
@$doc->loadHTML($html);
if(!$doc) {
throw new Exception("Failed to load $url");
}
$element = $doc->getElementById($id);
if(!$element) {
throw new Exception("An element with id $id was not found");
}
// get all object tags
$objects = $element->getElementsByTagName('object'); // return node list
// take the the value of the data attribute from the first object tag
$data = $objects->item(0)->getAttributeNode('data')->value;
// cut away the unnecessary parts and return the info
return substr($data, strpos($data, '=')+1);
}
// call it:
$finalcontent = getElementByIdAsString($html, 'mainclass');
print_r ($finalcontent);
【问题讨论】:
-
你提到错误......它们是什么?
-
它只是空白。有没有更好的方法让我得到错误?这一切都是新手
-
我只是想放置一个要抓取的 URL,而不是那个家伙在堆栈溢出时所做的 $html 示例
-
首先,删除
@,因为这会消除错误(避免使用它,真的)。然后添加error_reporting(E_ALL);报告所有错误。 -
我得到的唯一错误是在 Chrome 控制台中“加载资源失败:服务器响应状态为 500(内部服务器错误)”它没有加载我的 wordpress 页脚,所以我假设它只是在抓取期间导致错误。