phpsymfonyweb-scrapinggouttedomcrawler

Can't Scrape Attribute of <Input> from Sibling Element


I am trying to scrape some data using Symfony2, Goutte, and DomCrawler. I have a tricky situation where I need to get a value of an attribute inside a <td>.

Working section:

    $query = "//td[normalize-space(text()) = 'Event Title']/following-sibling::td[1]";
    $crawler->filterXPath($query)->each(function($crawler, $i) {
        echo $crawler->text();// . $i . PHP_EOL;
    });


<tr>
    <td>Event Title</td> 
    <td>the title is here</td> 
</tr>

well, now it's:

<tr>
    <td>Event Title</td> 
    <td><input value="thisiswhatIneed"></td> 
</tr>

And I'm trying to change the selector

$query = "//td[normalize-space(text()) = 'Presenter']/following-sibling::td[1]/input[value]"; 

Any idea how to continue to traverse the so that I can access the <input> in order to get what it's attribute value="" is?


Solution

  • Since value is an input's attribute, you need slghtly change your xpath query:

    $query = "//td[normalize-space(text()) = 'Presenter']/following-sibling::td[1]/input/@value"; 
    

    See example here.