V
V
Vitya Podpriklopolny2018-10-13 17:50:30
PHP
Vitya Podpriklopolny, 2018-10-13 17:50:30

phpQuery not working?

There is a code that parses the names of products on the Eldorado website (in theory, it should parse)

<?php

require_once('lib/phpQuery-onefile.php');

function get_content($url) {
  $ch = curl_init($url);
  curl_setopt($ch , CURLOPT_RETURNTRANSFER , true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  //curl_setopt($ch , CURLOPT_HEADER , true);
  //curl_setopt ($ch , CURLOPT_USERAGENT , $_SERVER['HTTP_USER_AGENT']);
  $result = curl_exec($ch);
  $result = iconv('windows-1251', 'UTF-8', $result);
  return $result;
}

$file = get_content('https://www.eldorado.ru/cat/4005/');

$doc = phpQuery::newDocument($file);

$forecast = $doc->find('.listGood')->children('.item');
//print_r($forecast);

foreach($forecast as $val) {
  $pq = pq($val);
  echo $pq->find('.itemTitle a')->text().'<br>';
}


phpQuery::unloadDocuments();

?>

Tried a bunch of things, it doesn't work.
It just gives a white screen and empty code. Tried through the DiDOM library , but the result is the same
$document = new Document($file);

$posts = $document->find('.listGood');

foreach($posts as $post) {
  echo $post->text(), "\n";
}

(I'm just trying to display products here)
And by the way, how reasonable is it to use the phpQuery library, which has not been updated for almost 10 years?
Help me please! PLEASE!!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2018-10-13
@dimonchik2013

Isn't it easier to take a ready-made example and adapt it?
for DOM there are all sorts of nokogiri, and for parsing - scrapy

A
AlexJN, 2019-06-29
@AlexJN

Might be useful to someone. phpQuery didn't work for me either. There was an error in the structure of the document. In particular, I had a closing tag at the very beginning of the document. After I removed this tag from the beginning of the line, everything worked.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question