Answer the question
In order to leave comments, you need to log in
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>";}
}
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question