简体   繁体   English

DomDocument / DOMXPath-如何通过itemprop和img src获取HTML Dom元素

[英]DomDocument/DOMXPath - How to get HTML Dom element by itemprop and img src

I am working on a script which is getting data from HTML DOM elements. 我正在研究从HTML DOM元素获取数据的脚本。

Here is my code: 这是我的代码:

$url = 'http://www.sportsdirect.com/nike-satire-mens-skate-shoes-242188?colcode=24218822';
libxml_use_internal_errors(true); 
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

$Name = $xpath->query('//span[@id="ProductName"]')->item(0)->nodeValue;

echo $Name;

This code is simply taking the text inside <span id="ProductName"></span> . 这段代码只是将文本包含在<span id="ProductName"></span> I know how to get the data from elements with specific class or id. 我知道如何从具有特定类或ID的元素中获取数据。

I don't know how I can get the src="http://adres-to-image.com/img.png" (pure example) from image tag or how I can get elements which do not have id or class but have attribute like itemprop , for example <div itemprop="name"></div> 我不知道如何从图片标记中获取src="http://adres-to-image.com/img.png" (纯示例),或者如何获取不具有id或class的元素具有类似于itemprop属性,例如<div itemprop="name"></div>

  1. How can I get the image src ? 如何获取src图片?
  2. How can I get elements with itemprop ? 如何使用itemprop获取元素?

For your examples: 举个例子:

$xpath->query('//img/@src)->item(0)->nodeValue

This means 这意味着

Select all src attributes of all img tags and get the value of the first 选择所有img标签的所有src属性,并获取第一个的值

$xpath->query('//div/[@itemprop="name"])->item(0)->nodeValue

This means 这意味着

Select all divs with itemprop attr equals name and get the value of the first. 选择所有具有itemprop attr等于名称的div,并获取第一个的值。

You just look for the attributes: 您只需要查找以下属性:

$url = 'http://www.sportsdirect.com/nike-satire-mens-skate-shoes-242188?colcode=24218822';
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

$Name = $xpath->query('//div[@class="productImageSash"]');
foreach($Name as $element){
    $imgs = $element->getElementsByTagName('img');
    foreach($imgs as $img){
        $src = $img->getAttribute('src');
        echo $src;
    }

}

Output: 输出:

/images/sash/productsash_mustgo.png 

The same with itemprop attribute, look for divs which have this attribute: itemprop属性相同,查找具有以下属性的div:

$Name = $xpath->query('//div');
foreach($Name as $element){
    $itemprop = $element->getAttribute('itemprop');
    if($itemprop){
        echo "found";
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM