从特定 div 获取所有图像 src
Fetching all images src from specific div
假设,我的 HTML 结构如下:
<div>
<div class="content">
<p>This is dummy text</p>
<p><img src="a.jpg"></p>
<p>This is dummy text</p>
<p><img src="b.jpg"></p>
</div>
</div>
我想从 .content
div 获取所有图像 src。我试过了:
<?php
// a new dom object
$dom = new domDocument;
// load the html into the object
$dom->loadHTML("example.com/article/2345");
// discard white space
$dom->preserveWhiteSpace = false;
//get element by class
$finder = new DomXPath($dom);
$classname = 'content';
$content = $finder->query("//*[contains(@class, '$classname')]");
foreach($content as $item){
echo $item->nodevalue;
}
但是,当我循环 $content
时,我什么也得不到。请帮助。
如下所示更改您的 XPath 查询:
// loading html content from remote url
$html = file_get_contents("http://nepalpati.com/entertainment/22577/");
@$dom->loadHTML($html);
...
$classname = 'content';
$img_sources = [];
// getting all images within div with class "content"
$content = $finder->query("//div[@class='$classname']/p/img");
foreach ($content as $img) {
$img_sources[] = $img->getAttribute('src');
}
...
var_dump($img_sources);
// the output:
array(2) {
[0]=>
string(68) "http://nepalpati.com/mediastorage/images/2072/Falgun/khole-selfi.jpg"
[1]=>
string(72) "http://nepalpati.com/mediastorage/images/2072/Falgun/khole-hot-selfi.jpg"
}
假设,我的 HTML 结构如下:
<div>
<div class="content">
<p>This is dummy text</p>
<p><img src="a.jpg"></p>
<p>This is dummy text</p>
<p><img src="b.jpg"></p>
</div>
</div>
我想从 .content
div 获取所有图像 src。我试过了:
<?php
// a new dom object
$dom = new domDocument;
// load the html into the object
$dom->loadHTML("example.com/article/2345");
// discard white space
$dom->preserveWhiteSpace = false;
//get element by class
$finder = new DomXPath($dom);
$classname = 'content';
$content = $finder->query("//*[contains(@class, '$classname')]");
foreach($content as $item){
echo $item->nodevalue;
}
但是,当我循环 $content
时,我什么也得不到。请帮助。
如下所示更改您的 XPath 查询:
// loading html content from remote url
$html = file_get_contents("http://nepalpati.com/entertainment/22577/");
@$dom->loadHTML($html);
...
$classname = 'content';
$img_sources = [];
// getting all images within div with class "content"
$content = $finder->query("//div[@class='$classname']/p/img");
foreach ($content as $img) {
$img_sources[] = $img->getAttribute('src');
}
...
var_dump($img_sources);
// the output:
array(2) {
[0]=>
string(68) "http://nepalpati.com/mediastorage/images/2072/Falgun/khole-selfi.jpg"
[1]=>
string(72) "http://nepalpati.com/mediastorage/images/2072/Falgun/khole-hot-selfi.jpg"
}