php Web-Scraping Divs with Class不显示图像

发布于 2025-02-08 10:11:13 字数 1069 浏览 3 评论 0原文

我正在网上网络上的报纸在线外部URL,使用此平面PHP,类block__Item,现在由其他类和图像组成

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
 
$url = "https://www.repubblica.it";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);

$doc = new DOMDocument('1.0', 'utf-8');
@$doc->loadHTML($content);


    $classname="block__item";
    $finder = new DomXPath($doc);
    $spaner = $finder->query("//*[contains(@class, '$classname')]");


foreach ($spaner as $value) {
    echo $doc->saveXML($value);
   }
/?>

,图像未显示;我在页面上有空间,即URL,但看不到图像。

我尝试了这个

$images = $finder->query("(//img/@src)");
foreach ( $images as $image) {
    $print = $doc->saveXML($image);
    echo $print;
}

,这给了我页面末尾的所有图像;
我尝试了联合收割机

foreach (array_combine($spaner, $images) as $value => $image){
    echo $value, $image;
   }

,但收到错误。 我也同时尝试了 Query 类和IMG,但没有。

拜托,有人可以帮我吗?

I'm web-scraping a newspaper online, external url, with this plane php, a class block__item, which is compose of other classes and images

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
 
$url = "https://www.repubblica.it";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);

$doc = new DOMDocument('1.0', 'utf-8');
@$doc->loadHTML($content);


    $classname="block__item";
    $finder = new DomXPath($doc);
    $spaner = $finder->query("//*[contains(@class, '$classname')]");


foreach ($spaner as $value) {
    echo $doc->saveXML($value);
   }
/?>

Now, images are not displayed; I have the space in the page, the url, but I can't see the image.

I tried with this

$images = $finder->query("(//img/@src)");
foreach ( $images as $image) {
    $print = $doc->saveXML($image);
    echo $print;
}

and this gave me all the images at the end of the page;
I tried with combine

foreach (array_combine($spaner, $images) as $value => $image){
    echo $value, $image;
   }

but I receive error.
I tried also with query class and img in the same time, but nothing.

Please, anyone could help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文