I’m new to Php and am trying to replicate classes I use in c# fro web-scraping.
I have built a standard Http Helper class for downloading pages etc. but I seek advice on grabbing elements as I can find no simple examples that don’t involve a loop that just outputs everything.
Here is the start
foreach ($nodes as $element) {
if($element->hasAttribute('class') && $element->getAttribute('class') == "itemgroup"){
$tempMessage = $element->getElementsByTagName('h3')->item(0)->nodeValue. "\n";
I dont want to have to loop through everything or guess the item order in the array of elements as above. I just want to be able to do a slight mix:
as with the dom Queries: "//div[@id='travel-itemlist']/div[@class='itemgroup']"
I want to be able to assign a value by getting a single element by attributes and values:
e.g: of a non working example:
$title = 'Title: ' . $e->getElementsByTagName('p')->item('class[@id='thatstheOne'')->nodeValue. "\n";
is this possible?
In short, you can’t do this like that in 1 line.
DOMXPath::query and DOMElement::getElementsByTagName both returns a DOMNodeList object that can be an empty list.
For really precise matching into the DOM, when you expect one result, I would use a XPath expression, then test the return value of the xpath function and assign a variable accordingly, for example:
using Ternary Operators here will help keep your code short and avoid an if-then-else soup.