php - 简单的 HTML DOM 库

标签 php simple-html-dom

在包含简单的 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 个错误:

fatal error :在第 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>';
?>

我做错了什么?

最佳答案

这是因为您的主机无法解析 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?

关于php - 简单的 HTML DOM 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11151782/

相关文章:

php - Doctrine 合并 : DateTime field always updated

php - 使用 HTTPful 请求 JSON 数据时如何获取数组而不是对象

php - 使用 cURL 的 session

php - html dom 解析 - 我可以只找到文件类型吗?

PHP 简单 HTML DOM 解析器内存泄漏

php - 如何使用 HTML DOM 将所有相对 URL 转换为绝对 URL?

php - 如何让链接 'index.php?x'在页面上显示x部分

php - Paypal 支付 REST API - REQUIRED_SCOPE_MISSING

php - 使用 simple.html.dom 进行抓取并将结果放入数组中

php - 使用简单的 HTML DOM 获取*绝对* URL