简体   繁体   English

类中的简单html dom空间

[英]simple html dom space in class

the following class I would like to reference using simple html dom 我想使用简单的html dom引用以下类

but there is 2 classes one 但是有2个班级

is

class="price"

the other appears to be class=" price" 另一个似乎是class=" price"

using this code does not appear to find it 使用此代码似乎找不到它

foreach ($html1->find('[class= price ]/text()',0) as $price_data2)

the source for the page in question is here 有关页面的来源在这里

http://www.amazon.com/Likeable-Social-Media-Irresistible-ebook/dp/B00511ONPG/ref=tmm_kin_title_0?ie=UTF8&qid=1367741120&sr=8-1 http://www.amazon.com/Likeable-Social-Media-Irresistible-ebook/dp/B00511ONPG/ref=tmm_kin_title_0?ie=UTF8&qid=1367741120&sr=8-1

An example with DOMDocument querying the class attribute value verbatim (with spaces around): DOMDocument的示例逐字查询类属性值(带有空格):

// configuration
libxml_use_internal_errors(true);

// input
$url = 'http://www.amazon.com/Likeable-Social-Media-Irresistible-ebook/dp/B00511ONPG/ref=tmm_kin_title_0?ie=UTF8&qid=1367741120&sr=8-1';

// processing
$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXPath($doc);
$prices  = $xpath->query("//*[@class=' price ']/text()");

// output
foreach($prices as $index => $price) {
    printf("%d: %s\n", $index, trim($price->textContent));
}

Output: 输出:

0: $14.81
1: $18.38
2: $11.58
3: --
4: 
5: 

Please note that the URL you gave contains invalid HTML. 请注意,您提供的网址包含无效的HTML。 Therefore the simpledom parser might produce different results (or does not work at all) with the data provided. 因此,简单性解析器可能会对提供的数据产生不同的结果(或根本不起作用)。 This is equally true for the DOMDocument object I use here, however, it is build on top of the pretty stable libxml library (not only used in the PHP world, but in very many other worlds as well) and it also has a recovery property which allows further control. 我在这里使用的DOMDocument对象也是如此,但是它建立在非常稳定的libxml库之上(不仅在PHP世界中使用,而且还在很多其他世界中使用),并且它还具有recovery属性这可以进一步控制。

You should be able to use: 您应该可以使用:

$html->find('*[class*=price]/text()')

I don't like that /text() though because it's not real css. 我不喜欢那个/text()因为它不是真正的CSS。

Also note that you need to leave out the ,0 when iterating with foreach . 还要注意,在使用foreach迭代时,您需要省略,0

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

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