$urlToScrap = "https://play.google.com/store/apps/details?id=flipboard.app#?t=W251bGwsMSwxLDIxMiwiZmxpcGJvYXJkLmFwcCJd";
$pageContentData = file_get_contents($urlToScrap);
$doc = new DOMDocument();
$doc->loadHTML($pageContentData);
$listOfDivs = $doc->getElementsByTagName("div");
foreach ($listOfDivs as $div) {
if($div->getAttribute("class") == "doc-banner-icon"){
$img = $div->getElementsByTagName("img");
var_dump($img->getAttribute("src"));
}
}
返回空。
我在dom中有以下元素:
<div class="doc-banner-icon"><img src="somesrc"></div>
我正在尝试获取 img src,由于页面中有很多图像,我想先获取父 div,然后提取其中的图像。
解决方案在这里:
$urlToScrap = "https://play.google.com/store/apps/details?id=flipboard.app#?t=W251bGwsMSwxLDIxMiwiZmxpcGJvYXJkLmFwcCJd";
$pageContentData = file_get_contents($urlToScrap);
$doc = new DOMDocument();
$doc->loadHTML($pageContentData);
$listOfDivs = $doc->getElementsByTagName("div");
foreach ($listOfDivs as $div) {
if($div->getAttribute("class") == "doc-banner-icon"){
$listOfImages = $div->getElementsByTagName("img");
foreach($listOfImages as $img){
var_dump($img->getAttribute("src"));
}
}
}