D
D
Daniil Sidorov2019-01-15 13:29:48
PHP
Daniil Sidorov, 2019-01-15 13:29:48

Why doesn't element search work (DiDOM library)?

Good afternoon. I'm trying to parse a page using GuzzleHttp and DiDOM .
First I find a group of elements. Then, inside each found element, you need to search.

$sportPage = $client->request('GET', 'Адрес сайта');

$response = (string) $sportPage->getBody();
$document = new DiDom\Document($response);

$cometitionQuery = $document->find('.родительский класс');

foreach ($cometitionQuery as $item)
{
    //Ругается на строчку ниже
    $competitionName = $item->find('.класс внутри выборки')->html();
    $competition[] = $competitionName;
}

As a result, I get the following error: I
Call to a member function html() on array
used PhpQuery before , when traversing an element, it was necessary to call the pq function in order to access the find(), html() methods... I tried to find something similar in the DiDOM documentation , but found nothing.
Please tell me what I'm doing wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DanKud, 2019-01-15
@DaniLaFokc

I do not know this library, but I do not think that it is fundamentally different from others. It also shows you an error that the method html()cannot be called on an array. By the method find()you get an array, even if it finds only one element, it will still be an array. Try like this:

.....
$competitionName = $item->find('.класс внутри выборки')[0]->html();
.....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question