search
HomeDaily ProgrammingPHP KnowledgePHP collection plug-in QueryList practical teaching (1)

1: Demand background

As a developer, you will more or less encounter the need for collection in daily development, and you need to collect data, articles, etc. from a certain website. etc. At the same time, it is also necessary to analyze and classify the collected content. In the process of parsing and classifying, most PHPers use regular methods to analyze the crawled data, which increases the difficulty and does not improve the efficiency. Using QueryList can solve the problem of results very easily.

2: What is QueryList?

QueryList is an open source project based on phpQuery, which allows PHPer to perform a JQuery-like DOM operation on the content when collecting information. It is very simple and convenient, and fits the usual development habits.

3: Download and install

(1) Environmental requirements, there are two installation methods, you can select any of the following.

PHP >= 5.3

(2) Installation method - Use composer to install

composer require jaeger/querylist:V3.2.1

or add the following configuration to the composer.json file of the project, and then composer update

"require" : {
  "jaeger/querylist": "^3.2"
}

After the installation is completed, In the project, directly introduce the vendor/autoload.php file and start using it directly

(3) Test

$html = <<<STR
<div id="one">
    <div class="two">
        <a href="http://querylist.cc">QueryList官网</a>
        <img src="/static/imghwm/default1.png"  data-src="http://querylist.com/1.jpg"  class="lazy"   alt="这是图片">
        <img src="/static/imghwm/default1.png"  data-src="http://querylist.com/2.jpg"  class="lazy"   alt="这是图片2">
    </div>
    <span>其它的<b>一些</b>文本</span>
</div>        
STR;
        $rules = array(
            //采集id为one这个元素里面的纯文本内容
            &#39;text&#39; => array(&#39;#one&#39;,&#39;text&#39;),
            //采集class为two下面的超链接的链接
            &#39;link&#39; => array(&#39;.two>a&#39;,&#39;href&#39;),
            //采集class为two下面的第二张图片的链接
            &#39;img&#39; => array(&#39;.two>img:eq(1)&#39;,&#39;src&#39;),
            //采集span标签中的HTML内容
            &#39;other&#39; => array(&#39;span&#39;,&#39;html&#39;)
        );

        $data = QueryList::Query($html, $rules)->data;
        var_dump($data);

(4) Installation method-manual installation

Go to Github Download the code https://github.com/jae-jae/QueryL, get the two files QueryList.php and phpQuery.php and put them into the project

(5) Test

<?php
require &#39;phpQuery.php&#39;;
require &#39;QueryList.php&#39;;

use QL\QueryList;

$hj = QueryList::Query(&#39;https://php.cn/&#39;,array("url"=>array(&#39;.unit h1 a&#39;,&#39;href&#39;)));

$data = $hj->getData(function($x){
    return $x[&#39;url&#39;];
});

print_r($data);

Summary, downloading and installing this plug-in is very simple. In the next issue, I will introduce simple collection of article content, which is suitable for beginners to learn. Everyone is welcome to pay attention and receive new course reminders in time.

The above is the detailed content of PHP collection plug-in QueryList practical teaching (1). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment