How to run php crawler

(*-*)浩
Release: 2023-02-27 11:44:01
Original
3226 people have browsed it

When it comes to making a crawler, the first thing that everyone may think of is Python. In fact, PHP can also be used to write crawler programs. PHP has always been simple and easy to use. I personally tested that I can write a simple crawler using the PHPspider framework.

How to run php crawler

##The matching method uses XPach syntax. (Recommended learning: PHP video tutorial)

<?php
require &#39;/vendor/autoload.php&#39;;
use phpspider\core\phpspider;

/* Do NOT delete this comment */
/* 不要删除这段注释 */

$configs = array(
&#39;name&#39; => &#39;简书&#39;,
&#39;log_show&#39; =>false,
&#39;tasknum&#39; => 1,
//数据库配置
&#39;db_config&#39; => array(
&#39;host&#39;  => &#39;127.0.0.1&#39;,
&#39;port&#39;  => 3306,
&#39;user&#39;  => &#39;root&#39;,
&#39;pass&#39;  => &#39;&#39;,
&#39;name&#39;  => &#39;demo&#39;,
),
&#39;export&#39; => array(
&#39;type&#39; => &#39;db&#39;,
&#39;table&#39; => &#39;jianshu&#39;,  // 如果数据表没有数据新增请检查表结构和字段名是否匹配
),
//爬取的域名列表  
&#39;domains&#39; => array(
    &#39;jianshu&#39;,
    &#39;www.jianshu.com&#39;
), 
//抓取的起点
&#39;scan_urls&#39; => array(
    &#39;https://www.jianshu.com/c/V2CqjW?utm_medium=index-collections&utm_source=desktop&#39;
),
//列表页实例
&#39;list_url_regexes&#39; => array(
    "https://www.jianshu.com/c/\d+"
),
//内容页实例
//  \d+  指的是变量
&#39;content_url_regexes&#39; => array(
    "https://www.jianshu.com/p/\d+",
),
&#39;max_try&#39; => 5,

&#39;fields&#39; => array(
    array(
        &#39;name&#39; => "title",
        &#39;selector&#39; => "//h1[@class=&#39;title&#39;]",
        &#39;required&#39; => true,
    ),
    array(
        &#39;name&#39; => "content",
        &#39;selector&#39; => "//div[@class=&#39;show-content-free&#39;]",
        &#39;required&#39; => true,
    ),
),
);

$spider = new phpspider($configs);
$spider->start();
Copy after login

After finishing the code, remember to establish the corresponding database and data table according to the content to be captured, and the fields must be aligned .

Then cmd, enter

php -f d:\jianshu\spider.php
Copy after login

and run as follows

How to run php crawler

The above is the detailed content of How to run php crawler. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template