【问题标题】:Get image URL inside an element from external website - Laravel从外部网站获取元素内的图像 URL - Laravel
【发布时间】:2021-09-09 19:37:25
【问题描述】:

正如我提到的问题here。我将尝试获取图片网址的替代方法。我想从https://www.matchesfashion.com/products/Adidas-By-Stella-McCartney-Metallic-zebra-print-Primegreen-leggings-1424516 获取产品图片网址,如果您检查产品图片,它可以在<figure></figure> 元素内访问。我做了一些研究并编写了这段代码来从外部网页获取内容。但它没有返回任何东西。

$doc = new DOMDocument;


$doc->preserveWhiteSpace = false;


$doc->strictErrorChecking = false;
$doc->recover = true;

$doc->loadHTMLFile('https://www.matchesfashion.com/products/Adidas-By-Stella-McCartney-Metallic-zebra-print-Primegreen-leggings-1424516');

$xpath = new DOMXPath($doc);

$var = $xpath->evaluate('string(//figure[@class="iiz"])');

我只需要获取该图像的源 URL,这样我就可以继续我的图像编码过程。提前致谢

【问题讨论】:

  • 正确的图片url好像是https://assetsprx.matchesfashion.com/img/product/920/1424516_1.jpg你为什么不用呢?如果你想要整张幻灯片,最好使用 iframe。
  • @PrafullaKumarSahu 这就是我的目标。获取该网址。我只有需要获取'assetsprx.matchesfashion.com/img/product/920/1424516_1.jpg'的产品页面的网址
  • 那么你可以搜索带有iiz__img 类的img元素。

标签: php html laravel domcrawler


【解决方案1】:

您好,您可以使用下面的代码来获取图片网址

 $doc = new DOMDocument;
        $doc->preserveWhiteSpace = false;
        $doc->strictErrorChecking = false;
        $doc->recover = true;

        ini_set('user_agent', 'My-Application/2.5');
        libxml_use_internal_errors(true);
        $doc->loadHTMLFile('https://www.matchesfashion.com/products/Adidas-By-Stella-McCartney-Metallic-zebra-print-Primegreen-leggings-1424516');
        $xpath = new DOMXPath($doc);
        $imgs  = $xpath->query('//*[@class="iiz__img "]');
        foreach($imgs as $img)
        {
            echo 'ImgSrc: https:' . $img->getAttribute('src') .'<br />' . PHP_EOL;
        }

这是您想要的结果

ImgSrc:https://assetsprx.matchesfashion.com/img/product/920/1424516_1.jpg

ImgSrc:https://assetsprx.matchesfashion.com/img/product/920/1424516_1.jpg

【讨论】:

  • @atikur rahman 我试图通过您回答的方法获得更多高级标签。但我得到空值。我想我接近了,但似乎没有得到想要的结果。请看一下是否可以stackoverflow.com/questions/69127818/…提前谢谢。
  • 嘿@DevinY 感谢再次寻求帮助,尽管它的问题不同,不管我也帮助了那个链接。祝你好运
猜你喜欢
  • 2014-01-24
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多