Question

I'm just getting to grips with QueryPath after using HTML Simple Dom for quite some time and am finding that the QP documentation doesn't seem to offer much in the way of examples for all of its functions.

At the moment I'm trying to retrieve some text from a HTML doc that doesn't make much use of ID's or Classes, so I'm a little outside of my comfort zone.

Here's the HTML:

<div class="blue-box">
  <div class="top">
    <h2><img src="pic.gif" alt="Advertise"></h2>
    <p>Some uninteresting stuff</p>
    <p>More stuff</p>
  </div>
</div>
<div class="blue-box">
  <div class="top">
    <h2><img src="pic2.gif" alt="Location"></h2>
    **I NEED THIS TEXT**
    <div style="margin:stuff">
      <img src="img3.gif">
    </div>
  </div>
</div>

I was thinking about selecting the class 'box-blue' as the starting point and then descending from there. The issue is that there could be any number of box-blue classes in the HTML doc.

Therefore I was thinking that maybe I should try to select the image with alt="Location" and then use ->next()->text() or something along those lines?

I've tried about 15 variations os far and none are getting the text I need.

Assistance most appreciated!

Was it helpful?

Solution

Can you have a look to this example http://jsfiddle.net/Pedro3M/mujtk/

I made like you said using the alt attribute, if you confirm if this is always unique

$("img[alt='Location']").parent().parent().text();

OTHER TIPS

How about:

$doc->find('div.top:has(img[alt="Location"])')->text();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top