如何在PHP中逐页获取图像URL

时间:2017-05-16 07:49:05

标签: php

这是我的代码:

<form method="POST">
    <input name="link">
    <button type="submit">></button>
</form>
<title>GET IMAGE URL</title>
<?php 
if (!isset($_POST['link'])) exit();
$link = $_POST['link'];
$parse = explode('.html', $link);
echo '<div id="pin" style="float:center"><textarea class="text" cols="110" rows="50">';
for ($i = 1; $i <=5; $i++)
{
    if ($i > 1)
    $link = "$parse[0]-$i.html";
    $get = file_get_contents($link);
    if (preg_match_all('/src="(.*?)"/', $get, $matches))
    {
        foreach ($matches[1] as $content)
        echo $content."\r\n";
    }
}
echo '</textarea>';

我尝试获取img src的页面有10到15页,所以我想让我的代码获取所有img url直到页面结束。如果没有循环,我怎么能这样做?

如果我使用:

for ($i = 1; $i <=5; $i++)

这将只获得5页img网址,但我想让它一直到最后。然后,每当我提交另一个包含不同页数的网址时,我都不需要编辑循环。

2 个答案:

答案 0 :(得分:1)

从此

  

这将只获得5页img网址,但我想让它一直到最后。然后,每当我提交另一个包含不同页数的网址时,我都不需要编辑循环。

我可以理解您的问题与动态页数有关。您的网址底部有下一页链接

                下一页             

识别它并在while循环中获取图像

<?php

// Link given in form
$link = "http://www.xiumm.org/photos/XiuRen-17305.html";
$parse = explode('.html', $link);
$i=1;
// Intialize a boolean 
$nextPageFound = true;


while($nextPageFound) {
    // Construct URL Every time when nextPageFound
    if ($i == 1) {
        $url = "$parse[0].html";
        echo "First Page<br><br>";
     } else {
        $url = "$parse[0]-$i.html";
       }

    // Getting URL Contents
    $get = file_get_contents($url);
    if (preg_match_all('/src="(.*?)"/', $get, $matches))
    {
    // echoing contents
    foreach ($matches[1] as $content)
    echo $content."<br>";
    }
    // check nextPageBtn if available 
    if (strpos($get, '"nextPageBtn"') !== false) {
     $nextPageFound = true;
     // increment +1
     $i++;
    echo "<br>Page $i<br><br>";
    } else {
     $nextPageFound = false;
     echo "THE END";
    }

}
?>

答案 1 :(得分:0)

您应该使用HTML {XML解析器,例如DOMDocument,并结合DOMXPathxpath是查询语言来查询(X)HTML数据结构):

// create DOMDocument
$doc = new DOMDocument();
// load remote HTML file
$doc->loadHTMLFile( $link );

// create DOMXPath
$xpath = new DOMXPath( $doc );
// fetch all IMG elements that have a src attribute
$nodes = $xpath->query( '//img[@src]' );

// loop trough found IMG elements and echo their src attribute values
for( $i = 0; $i < $nodes->length; $i++ ) {
  echo $nodes->item( $i )->getAttribute( 'src' ) . PHP_EOL;
}

关于@Enuma在评论中提到的xpath查询//div[contains(@class,'pic_box')]//@src

该查询的结果DOMNodeList将不包含DOMElement个对象,而是DOMAttr个对象,因为查询直接请求属性,而不是元素。由于DOMAttr表示属性而不是元素,因此方法getAttribute()不存在。要获取属性的值,您必须使用属性DOMAttr->value

因此,我们必须稍微改变上面示例代码的相关部分:

// loop trough found src attributes and echo their value
for( $i = 0; $i < $nodes->length; $i++ ) {
  echo $nodes->item( $i )->value . PHP_EOL;
}

总而言之,我们的示例代码变为:

// create DOMDocument
$doc = new DOMDocument();
// load remote HTML file
$doc->loadHTMLFile( $link );

// create DOMXPath
$xpath = new DOMXPath( $doc );
// fetch all src attributes that are descendants of div.pic_box 
$nodes = $xpath->query( '//div[contains(@class,'pic_box')]//@src' );

// loop trough found src attributes and echo their value
for( $i = 0; $i < $nodes->length; $i++ ) {
  echo $nodes->item( $i )->value . PHP_EOL;
}

PS。:为了使DOMDocument能够加载远程文件,我相信可能需要设置一些php配置设置,我不知道我的头脑, 马上。但是因为它似乎已经为@Enuma工作了,所以它现在并不重要。也许我稍后会查看它们。