This is the first time i am using PHP DOMDocument and i don't know its methods.
I grab the html that has the following format
<div class=row abc>...</div>
<div class=row xyz>...</div>
<div class=row qrs>...</div>
...
...
<div class="row>This is what i want to grab</div>
<div class="row show-more-result">Show More</div>
What i am trying to achieve is that first i select the div with class show-more-results
and then target the one level upper div thats where my data is present.
I have started exploring the PHP DOMDocument class but there is not any getElementByClass
method i found
public function scrapping()
{
// Create a DOMDocument Object to fetch the search results
$dom = new \DOMDocument;
@$dom->loadHTML($this->_response);
$dom->preserveWhiteSpace = false;
$xpath = new \DomXpath($dom);
$show_more_div = $xpath->query('//*[@class="show-more-result"]')->item(0);
$stuff = $show_more_div->textContent;
echo($stuff);
}
I tried to target the show more div but it says Trying to get property of non-object
as if the $xpath-query()
returns nothing.
Please help me in targeting the desired div.
Updated
var_dump($xpath->query('//*[@class="show-more-result"]')->item(0));
// NULL
答案 0 :(得分:1)
You're doing a straight string equality:
$show_more_div = $xpath->query('//*[@class="show-more-result"]')->item(0);
^^^^^^^
But your target div's class is actually row show-more-result
. You need to do a substring match instead:
//*[contains(@class, 'show-more-result')]