PHP DOM文档错误处理

发布于 2024-10-09 12:19:09 字数 803 浏览 0 评论 0原文

我在尝试为 DOM 编写一个 if 语句来检查 $html 是否为空时遇到问题。然而,每当 HTML 页面最终变成空白时,它只会删除 DOM 下面的所有内容(包括我必须检查它是否为空白的内容)。

$html = file_get_contents("http://example.com/");
$dom = new DOMDocument;
@$dom->loadHTML($html);
$links = $dom->getElementById('dividhere')->getElementsByTagName('img');
foreach ($links as $link)
{
    echo $link->getAttribute('src');
}

这一切所做的就是在指定的 div 中抓取一个图像 URL,它可以完美地工作,直到该页面是一个空白的 HTML 页面。

我尝试过使用 SimpleHTMLDOM,但它也不起作用(它甚至没有在工作页面上获取图像)。我是否碰巧错过了这个,或者我只是错过了两者的某些东西?

include_once('simple_html_dom.php')
$html = file_get_html("http://example.com/");
foreach($html->find('div[id="dividhere"]') as $div)
{
    if(empty($div->src))
    {
        continue;
    }
    echo $div->src;
}

I'm having trouble trying to write an if statement for the DOM that will check if $html is blank. However, whenever the HTML page does end up blank, it just removes everything that would be below DOM (including what I had to check if it was blank).

$html = file_get_contents("http://example.com/");
$dom = new DOMDocument;
@$dom->loadHTML($html);
$links = $dom->getElementById('dividhere')->getElementsByTagName('img');
foreach ($links as $link)
{
    echo $link->getAttribute('src');
}

All this does is grab an image URL in the specified div, which works perfectly until the page is a blank HTML page.

I've tried using SimpleHTMLDOM, which didn't work either (it didn't even fetch the image on working pages). Did I happen to miss something with this one or am I just missing something in both?

include_once('simple_html_dom.php')
$html = file_get_html("http://example.com/");
foreach($html->find('div[id="dividhere"]') as $div)
{
    if(empty($div->src))
    {
        continue;
    }
    echo $div->src;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2024-10-16 12:19:09

摆脱 $html 变量,只需通过执行 @$dom->loadHTMLFile("http://example.com/"); 将文件加载到 $dom 中,然后使用 if 语句下面检查 $dom 是否为空。

Get rid on the $html variable and just load the file into $dom by doing @$dom->loadHTMLFile("http://example.com/");, then have an if statement below that to check if $dom is empty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文