1

使用 PHP Simple HTML DOM Parse 但无法获取要显示的图像。

我不是编码员,我正在尝试从网站上提取文章和图像。文章很好,但图像不显示。而是显示部分路径,例如

> //ssl.gstatic.com/ui/v1/button/search-white.png
> //ssl.gstatic.com/ui/v1/button/search-white.png
> //ssl.gstatic.com/ui/v1/icons/common/settings.png

以谷歌为例,这是我正在使用的代码:

<?php 
$html = file_get_html('https://news.google.com/nwshp?hl=en&tab=in');  

foreach($html->find('h2') as $e) 
     echo $e->innertext . '<br><br>';  

foreach($html->find('div.jsdisplay') as $e) 
     echo $e->innertext . '<br>'; 

foreach($html->find('img') as $element) 
     echo $element->src . '<br>';
?>

谢谢你的帮助

4

3 回答 3

2

你应该更换

foreach($html->find('img') as $element)
    echo $element->src . '<br>';

foreach ( $html->find('img') as $element ) {
    $img = str_replace(array("//ssl"), array("http://ssl"), $element->src);
    for($i = 0; $i < 5; $i ++) {
        $img = str_replace("//nt$i", "http://nt$i",$img);
    }
    echo "<img src=\"$img\"  /> <br>";
}
于 2012-10-15T16:48:21.650 回答
0

//ssl.gstatic.com/ui/v1/button/search-white.png是一个相对 URI(未指定方案,因此它将使用与它出现的页面相同的方案(例如 http: 或 https:))。

像解决任何其他相对 URI 一样解决它。

我的问题是如何使用我原始帖子中的代码显示图像。

您必须将<img>标签而不是 URI 作为纯文本输出。

于 2012-10-15T16:43:57.590 回答
0

在您最后发表评论后使用您的原始网站网址“http://frielatvsales.com/QuadAttachments.htm”更新我的答案

试试下面的代码。

include_once "simplehtmldom/simple_html_dom.php";


$url = "http://frielatvsales.com/QuadAttachments.htm";

$html = file_get_html($url); 

preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);

$host = $matches[1];

foreach($html->find('h2') as $e) {

echo $e->innertext . '<br><br>';  
}

foreach($html->find('div.jsdisplay') as $e) {

echo $e->innertext . '<br>'; 

}

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

echo '<img src=http://'.$host.'/'.$element->src . ' /><br>';
}
于 2012-10-15T16:50:42.300 回答