Home  >  Article  >  Backend Development  >  How to crawl data in php (with code)

How to crawl data in php (with code)

不言
不言Original
2018-09-15 17:06:0410042browse

The content of this article is about how to implement crawling data in PHP (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Official website: Simple, flexible and powerful PHP collection tool, making collection easier.

Introduction:
QueryList uses jQuery selector for collection, allowing you to bid farewell to complex regular expressions; QueryList has the same DOM operation capabilities, Http network operation capabilities, garbled code resolution capabilities, and content filtering capabilities as jQuery And scalability; it can easily implement complex network requests such as simulated login, fake browser, HTTP proxy, etc.; it has rich plug-ins, supports multi-threaded collection and uses PhantomJS to collect JavaScript dynamically rendered pages.

Installation

Installation through Composer:

composer require jaeger/querylist

Usage tutorial:

Direct code:

  <?php
include &#39;./vendor/autoload.php&#39;;
// 使用composer安装后引入目录
use QL\QueryList;
// 使用插件

$html = file_get_contents(&#39;https://www.biqudu.com/14_14778/&#39;);
// 手动获取页面
$data = QueryList::html($html);
// 得到页面内容
$data = QueryList::setHtml(&#39;https://www.biqudu.com/14_14778/&#39;);
// 等同于上面的html()
$data->rules([
    // 采集所有a标签的href属性
    &#39;link&#39; => [&#39;a&#39;,&#39;href&#39;],
    // 采集所有a标签的文本内容
    &#39;text&#39; => [&#39;a&#39;,&#39;text&#39;]
    ]);
// 此处$data = 上面已经获取到网页内容之后的对象
// 设置采集规则 替代了传统正则
$data->query();
// 此处$data = 上面已经获取到网页内容之后的对象 
// query 执行操作
$data->getData();
// 此处$data = 上面已经获取到网页内容之后的对象
// 得到数据结果
$data->all();
// 此处$data = 上面已经获取到网页内容之后的对象
// 将数据转换成二维数组
print_r($data->all());
// 打印结果

The basic usage method above is This way we can already capture a certain amount of data

Related recommendations:

Detailed explanation of the steps for php to crawl Tmall and Taobao product data (with code)

PHP code to implement crawler recording is super useful, php code crawler

The above is the detailed content of How to crawl data in php (with code). 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

Related articles

See more