【问题标题】:Simple HTML DOM Library简单的 HTML DOM 库
【发布时间】:2012-06-24 11:48:16
【问题描述】:

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

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

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

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

  $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>';
?>

我做错了什么??

【问题讨论】:

  • 这不是库问题,PHP 只是无法将主机解析为 IP 地址
  • @Baszz 你指的是哪个文件? DOM 文件在那里,它也包括在内
  • @user1458514 查看您的 Internet Explorer 代理设置

标签: php simple-html-dom


【解决方案1】:

这是因为您的主机无法解析 DNS,当 simplehtmldom 使用 file_get_contents 而不是 curl 时会发生这种情况。 PHP Simple HTML DOM Parser 是一个很棒的 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?

【讨论】:

  • 谢谢你..这听起来可能太业余了,但你能告诉我应该是什么输出..我得到一个空白屏幕..
  • 它应该按照您在脚本中的要求打印出所有链接(a->href)和图像(img->src)。我尝试了完全相同的脚本,并使用输出更新了 ans .
  • 你可能会得到一个空白屏幕,因为你可能没有在你的机器上启用 curl。启用卷曲。如果您不知道如何操作,请告诉我您的操作系统。
  • 打开您的 php.ini 文件...并搜索 curlxx.dll 并删除 ;在文件名前面,保存 php.ini 文件。重启阿帕奇。你在用 xampp 还是 wampp?
  • 按照这个:tildemark.com/programming/php/…重启你的apache并重新运行脚本
【解决方案2】:

这与simple_html_dom 没有任何关系。您的服务器无法访问 Internet,并且无法解析 google.com。检查 DNS 设置,可能还有防火墙。

【讨论】:

  • 服务器可以上网,仍然收到这个错误信息。有什么线索吗?
  • 这是因为您的主机无法解析 DNS,当 simplehtmldom 使用 file_get_contents 而不是 curl 时会发生这种情况。
猜你喜欢
  • 2023-03-04
  • 1970-01-01
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-03
相关资源
最近更新 更多