您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php爬虫怎么运行

2025/9/19 20:19:08发布28次查看
说到做爬虫,大家都可能第一时间想到的是python,其实php也是可以用来写爬虫程序的。php一贯简洁、易用,亲测使用phpspider框架能写出一个简单的爬虫。
匹配方式使用xpach语法。  (推荐学习:php视频教程)
<?phprequire '/vendor/autoload.php';use phpspider\core\phpspider;/* do not delete this comment *//* 不要删除这段注释 */$configs = array('name' => '简书','log_show' =>false,'tasknum' => 1,//数据库配置'db_config' => array('host' => '127.0.0.1','port' => 3306,'user' => 'root','pass' => '','name' => 'demo',),'export' => array('type' => 'db','table' => 'jianshu', // 如果数据表没有数据新增请检查表结构和字段名是否匹配),//爬取的域名列表 'domains' => array( 'jianshu', 'www.jianshu.com'), //抓取的起点'scan_urls' => array( 'https://www.jianshu.com/c/v2cqjw?utm_medium=index-collections&utm_source=desktop'),//列表页实例'list_url_regexes' => array( "https://www.jianshu.com/c/\d+"),//内容页实例// \d+ 指的是变量'content_url_regexes' => array( "https://www.jianshu.com/p/\d+",),'max_try' => 5,'fields' => array( array( 'name' => "title", 'selector' => "//h1[@class='title']", 'required' => true, ), array( 'name' => "content", 'selector' => "//div[@class='show-content-free']", 'required' => true, ),),);$spider = new phpspider($configs);$spider->start();
打完代码后,记得根据要抓取的内容建立对应的数据库、数据表,字段要能对对上。
接着cmd,输入
php -f d:\jianshu\spider.php
运行如下
以上就是php爬虫怎么运行的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product