繁体   English   中英

使用 PHP 从特定 URL 中提取所有图库图像

[英]Extract all the gallery images from specific URL with PHP

我正在尝试从特定 URL 中提取所有图像并将其存储在特定文件夹中。 我试着做一些研究,但我能得到的只是一些图像列表。

<?php
// $url_image = $_GET['url'];
$url_image = 'https://www.thebridesofoklahoma.com/wedding-inspiration/elegant-yet-modern/';
$homepage = file_get_contents($url_image);
preg_match_all("{<img\\s*(.*?)src=('.*?'|\".*?\"|[^\\s]+)(.*?)\\s*/?>}ims", $homepage, $matches, PREG_SET_ORDER);
// print_r($matches);
foreach ($matches as $val) {

    $pos = strpos($val[2],"/"); 
    $link = substr($val[2],1,-1);
    if($pos == 1)
        echo "https://www.thebridesofoklahoma.com" . $link;
    else
        echo $link;
    echo "<br>";
}
?>

谁能帮我获取仅在画廊中使用的图像 url 的所有列表。 请参考此网页: https://www.thebridesofoklahoma.com/wedding-inspiration/elegant-yet-modern/

不要使用 Regex 解析 HTML。 使用 DomDocument。 可用的图像被包装到一个 div 中,因此使用 XPath 很容易找到它们。

libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile('https://www.thebridesofoklahoma.com/wedding-inspiration/elegant-yet-modern/');
$img = new DOMXPath($doc);
foreach($img->query('//div/img') as $image) {
    echo $image->getAttribute('src'), PHP_EOL;
}
https://images.thebridesofoklahoma.com/wp-content/uploads/2017/08/21161012/Logo-final-01-1024x339.jpg
https://images.thebridesofoklahoma.com/wp-content/uploads/profiles/1564/08234701/bl_bartending_horiz_reversed.jpg
https://images.thebridesofoklahoma.com/wp-content/uploads/2021/01/26203513/Screen-Shot-2021-01-26-at-8.32.49-PM-850x1024.png
https://images.thebridesofoklahoma.com/wp-content/uploads/profiles/65/10015531/static.squarespace.com_.jpg
https://images.thebridesofoklahoma.com/wp-content/uploads/2019/09/17131459/IMG_2157-1024x965.jpg
https://images.thebridesofoklahoma.com/wp-content/uploads/2021/01/04143016/kgc-photography-logo-copy-1024x479.png
https://images.thebridesofoklahoma.com/wp-content/uploads/2021/03/25104302/FB4A0746-420x290.jpg
https://images.thebridesofoklahoma.com/wp-content/uploads/2021/03/24120412/harvard-99-420x290.jpg
https://images.thebridesofoklahoma.com/wp-content/uploads/2021/03/22133337/Aaron_Snow_Photography_Hall_Wedding.AES_0256-420x290.jpg

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM