我正在使用http://simplehtmldom.sourceforge.net中的simple_html_dom.php来获取维基百科页面上所有图片的完整网址。我主要是寻找公司和组织。下面的脚本适用于少数但我得到致命错误:在非对象上调用成员函数find()...对于此示例YouTube中的许多搜索以及我在其他人中尝试Facebook。我知道这是因为$ html不是一个对象。返回网址最有成功的方法是什么?请参阅下面的代码。非常感谢任何帮助。
<html>
<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" value="YouTube"/>
<input type="submit" value="Submit">
</form>
<?php
include 'simple_html_dom.php';
if (isset($_POST['q']))
{
$search = $_POST['q'];
$search = ucwords($search);
$search = str_replace(' ', '_', $search);
$html = file_get_html("http://en.wikipedia.org/wiki/$search");
?>
<h2>Search results for '<?php echo $search; ?>'</h2>
<ol>
<?php
foreach ($html->find('img') as $element): ?>
<?php $photo = $element->src;
echo $photo;
?>
<?php endforeach;
?>
</ol>
<?php
}
?>
</body>
</html>
我现在已经按照以下评论中的建议(虽然我可能犯了一个错误)并在点击“提交”时遇到错误:
警告:DOMDocument :: loadHTMLFile():ID ref_media_type_table_note_2已在http://en.wikipedia.org/wiki/YouTube中定义,行:270 in ...
警告:DOMDocument :: loadHTMLFile():ID ref_media_type_table_note_2已在http://en.wikipedia.org/wiki/YouTube中定义,行:501 in ...
请参阅下面我修改的代码:
<html>
<body>
<form method="post"> Search:
<input type="text" name="q" value="YouTube"/>
<input type="submit" value="Submit"> </form>
<?php
if (isset($_POST['q']))
{ $search = $_POST['q'];
$search = ucwords($search);
$search = str_replace(' ', '_', $search);
$doc = new DOMDocument();
$doc->loadHTMLFile("http://en.wikipedia.org/wiki/$search");
foreach ($doc->getElementsByTagName('img') as $image)
echo $image->getAttribute('src');
}
?>
</body>
</html>
答案 0 :(得分:1)
@
来阻止它们。file_get_html
问题来解决
卷曲。