PHP DOM文档错误处理
我在尝试为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
摆脱 $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.