簡體   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