简单的HTML DOM库

时间:2012-06-22 07:19:58

标签: php simple-html-dom

在包含简单的HTML DOM库时,我收到警告:

警告:file_get_contents()[function.file-get-contents]:php_network_getaddresses:getaddrinfo failed:没有这样的主机。在第70行的C:\ xampp \ htdocs \ simple_html_dom.php

警告:file_get_contents(http://www.google.com/)[function.file-get-contents]:无法打开流:php_network_getaddresses:getaddrinfo failed:没有这样的主机已知。在第70行的C:\ xampp \ htdocs \ simple_html_dom.php

simple_html_dom.php文件中的第70行(从http://sourceforge.net/projects/simplehtmldom/files/latest/download下载)是

  $contents = file_get_contents($url, $use_include_path, $context, $offset);

还有1个错误:

致命错误:在第15行的C:\ xampp \ htdocs \ domdoc2.php中调用非对象的成员函数find()

代码的第15行(下面)是

foreach($html->find('img') as $element) 

我在下面的代码中引用的网页是google.com 代码如下:

     <?php

include('simple_html_dom.php');
$html = new simple_html_dom();  
$html = file_get_html('http://www.google.com/');
// Find all images 
foreach($html->find('img') as $element) 
       echo $element->src . '<br>';

// Find all links 
foreach($html->find('a') as $element) 
       echo $element->href . '<br>';
?>

我做错了什么?

2 个答案:

答案 0 :(得分:1)

这与simple_html_dom没有任何关系。您的服务器无法访问互联网,但无法解析google.com。检查DNS设置,也许还有防火墙。

答案 1 :(得分:1)

这是因为您的主机无法解析DNS,当simplehtmldom使用file_get_contents而不是curl时会发生这种情况。 PHP简单的HTML DOM解析器是一个很好的HTML解析PHP类,但它使用起来很慢 file_get_contents(几乎在所有配置上都被禁用)而不是cURL(快4-5倍,有很多选项,几乎每个服务器都有它)。

只更换了file_get_contents,因此您可以安全地覆盖以前的版本,一切都会像以前一样工作,只会更快

源代码链接: http://webarto.com/static/download/simple_html_dom.rar

//output should be

/intl/en_ALL/images/srpr/logo1w.png
http://www.google.com/webhp?hl=en&tab=ww
http://www.google.com/imghp?hl=en&tab=wi
http://maps.google.com/maps?hl=en&tab=wl
https://play.google.com/?hl=en&tab=w8
http://www.youtube.com/?tab=w1
http://news.google.com/nwshp?hl=en&tab=wn
https://mail.google.com/mail/?tab=wm
https://docs.google.com/?tab=wo
http://www.google.com/intl/en/options/
https://www.google.com/calendar?tab=wc
http://translate.google.com/?hl=en&tab=wT
http://www.google.com/mobile/?tab=wD
http://books.google.com/bkshp?hl=en&tab=wp
https://www.google.com/offers/home?utm_source=xsell&utm_medium=products&utm_campaign=sandbar&tab=wG#!details
https://wallet.google.com/manage/?tab=wa
http://www.google.com/shopping?hl=en&tab=wf
http://www.blogger.com/?tab=wj
http://www.google.com/reader/?hl=en&tab=wy
http://www.google.com/finance?tab=we
http://picasaweb.google.com/home?hl=en&tab=wq
http://video.google.com/?hl=en&tab=wv
http://www.google.com/intl/en/options/
https://accounts.google.com/ServiceLogin?hl=en&continue=http://www.google.com/
http://www.google.com/preferences?hl=en
/preferences?hl=en
/url?sa=p&pref=ig&pval=3&q=http://www.google.com/ig%3Fhl%3Den%26source%3Diglk&usg=AFQjCNFA18XPfgb7dKnXfKz7x7g1GDH1tg
http://www.google.com/history/optout?hl=en
/advanced_search?hl=en
/language_tools?hl=en
/intl/en/ads/
/services/
https://plus.google.com/116899029375914044550
/intl/en/about.html
/intl/en/policies/

但是,如果您对PHP中的HTML解析完全陌生,请考虑阅读:How do you parse and process HTML/XML in PHP?