【问题标题】:PHP - failed to open stream: no such host is knownPHP - 无法打开流:没有这样的主机是已知的
【发布时间】:2013-04-27 19:31:10
【问题描述】:

您好,我在简单的 html dom 代码中遇到问题,它显示此错误:-

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

因为这个网址 http://www.arakne-links.c 现在我想要的不起作用

要知道有什么方法可以跳过不起作用的 url..

这是我正在使用的代码

ini_set('display_errors', 'on'); 
include_once('../../simple_html_dom.php');

// create HTML DOM

$htmls =  file_get_html('http://info.vilesilencer.com/top');    
foreach($htmls->find('a[rel="nofollow"]') as $e):
$test = $e->href;
$url  = array( $test );
$html = array();
foreach( $url as $key=>$value ) { 

// get html plain-text for webpage & assign to html array.

$html = file_get_html( trim($value) ); 

// echo html plain text:
echo $html->find('title', 0)->innertext;

}     
endforeach; 

请帮我解决这个问题。

谢谢你

【问题讨论】:

  • 你在哪里使用file_get_content函数。
  • 我修复它并进入下一个 url 但现在有新错误我替换此代码 $html = file_get_html( trim($value) ); // echo html 纯文本: echo $html->find('title', 0)->innertext;与 $html = @file_get_html( 修剪($value) ); if($html){ echo $html->find('title', 0)->innertext; }else{ //错误回显'不工作'; } 但现在它显示了一些通知
  • 你好小姐便便我在这里使用 file_get_content echo $html->find('title', 0)->innertext;它是一个 simple_html_dom.php 函数
  • 它显示了什么通知?

标签: php error-handling simple-html-dom


【解决方案1】:

在解析之前检查 URL 怎么样?

ini_set('display_errors', 'on'); 
include_once('simple_html_dom.php');

function urlOk($url) {
    $headers = @get_headers($url);
    if($headers[0] == 'HTTP/1.1 200 OK') return true;
    else return false;
}

// create HTML DOM

$htmls =  file_get_html('http://info.vilesilencer.com/top');    
foreach($htmls->find('a[rel="nofollow"]') as $e):
    $test = $e->href;
    $url  = array( $test );
    $html = array();
    foreach( $url as $key=>$value ) { 
       // get html plain-text for webpage & assign to html array.
       if (urlOk(trim($value))) {
           $html = file_get_html( trim($value) ); 
           echo $html->find('title', 0)->innertext;
           echo "<br />";
       } else {
         echo 'Error: URL '.$value.' doesn\'t exist.<br />';
       }
}     
endforeach; 
?>

【讨论】:

  • 谢谢,这正是我正在寻找的,但需要更多的小帮助,petra 我收到此错误通知:尝试在 D:\xampp\htdocs\scrap\example\ 中获取非对象的属性第 22 行上的 scraping\example_scraping_imdb.php .....它是在第 22 行中找不到标题标签时出现的。只有找到标题标签时,有没有办法在第 22 行工作
  • 找不到标题时是否需要“else”部分?否则,只需使用 error_reporting(E_ALL ^ E_NOTICE); 切断通知;
猜你喜欢
  • 1970-01-01
  • 2011-06-25
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
  • 2021-12-08
相关资源
最近更新 更多