Z
Z
ZaurK2016-06-24 11:48:11
PHP
ZaurK, 2016-06-24 11:48:11

How to check if a class exists on a page using phpquery?

Hello!
There is an index.html file which links to three other files one.html, two.html and three.html. The structure is extremely simple. Files one and three have a ul tag with the class list, but file two does not have that class or tag.
There is also a get.php file that parses these pages and checks for the presence of the .list class, and the answer is whether this class is present or not. Here is the code:

<?php

header('Content-type: text/html; charset=UTF-8');

$start = microtime(true);

set_include_path(get_include_path().PATH_SEPARATOR.'library/'); 
set_include_path(get_include_path().PATH_SEPARATOR.'phpQuery/'); 

require('config.php');

function __autoload( $className ) {require_once( "$className.php" );}

echo "<br>".date('H:i:s')." Начинаем парсинг ";
echo '<pre>';

$page=file_get_contents('index.html');
$document = phpQuery::newDocument($page);

$links=[];
foreach($document->find('ul li a') as $link){

$links[] = pq($link)->attr('href');
}
print_r($links);

foreach($links as $sublink){
$pageText =new Curl();
$pagenew=$pageText->get_page($sublink);
$cat_page = phpQuery::newDocument($pagenew);
$catlist = [];

foreach($cat_page as $cat_page){
if($item=pq($cat_page)->find('ul.list a')) {
    echo "class is</br>";}else{
    echo "class not is</br>";}
}

The problem is that in the end it is displayed that the class is on all three pages, although it is not on the second page. Please help me understand what is wrong here? here is what is output:
11:07:22 Начинаем парсинг
Array
(
[0] => one.html
[1] => two.html
[2] => three.html
)
class is
class is
class is

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2016-06-24
@springimport

First, the code is shit, sorry.
Second, the " class is " construct is inadequate.
Thirdly, you are looking for anything, not a class: find('ul.list a').
Fourth, look at Apist .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question