S
S
sania_002016-05-21 23:59:47
PHP
sania_00, 2016-05-21 23:59:47

What am I doing wrong with phpQuery?

Good day!
I want to parse fl curl in general - oh!
I get html, and it is correct, but phpQuery behaves in a strange way, I described it in more detail in the comments to the code.

<?php
require_once 'vendor/autoload.php';

// -------------------------- [ Main Code ] --------------------------

$url = 'https://www.fl.ru/projects/';

function parse_content($url=''){
    $ch = curl_init();

    $headers = [
        'User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
        'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
        'Connection:keep-alive',
    ];
    $cookie = dirname(__DIR__) . 'tmp/cookie.txt';

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);

    $res = curl_exec($ch);

    curl_close($ch);

    $html =  iconv("windows-1251","utf-8", $res);

    return $html;
}

/* echo parse_content($url); // Тут html парситься нормально

exit;*/


$result = phpQuery::newDocument(parse_content($url));

//echo $result->html(); // А тут уже отдается пол страницы с непонятными символами

// Далее вообще не могу получить ничего

foreach ($result->find('#projects-list .b-post') as $block) {
    $obj = pq($block);

    $title = $obj->find('h2')->html();
    $prise = $obj->find('.b-post__price')->text();
    $text = $obj->find('.b-post__txt ')->text();

    print $title
        ."<br /> Цена:". $prise
        ."<br />" .      $text ;
}


phpQuery::unloadDocuments();

Thank you for your attention!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#algooptimize #bottize, 2016-05-22
@sania_00

As I understand it, due to invalid markup
, phpQuery::newDocument
parses only half of the document.
If I'm right, then you need to find the wrong tag, the absence of a slash, quotes, etc.
Further, through replace, make a valid document and only then parse.
I don't have PHP) didn't run

S
sania_00, 2016-05-22
@sania_00

106696932a61405685dcb7e8585ebe01.png
Where do these smileys come from?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question