# php爬虫脚本
----
- 框架只做分发,不做数据处理,需要自己在回调中定制。
- 不限制采集方式,可以用正则,Xpath,字符串截取。
- 无限层级采集,可以实现列表->详情,列表->列表->详情,详情->详情等任意姿势采集。
- 队列去重,可以增量抓取,也可以全量采集。
- 支持调试模式,实时报表,守护模式。
## 安装依赖
| 环境 | 说明 |
|-------:|--------|
| php | >5.6,最好是php7以上 |
| redis |数据队列 |
## 快速开始
### 1、复制代码到index.php文件中
```php
if (!is_file('./vendor/autoload.php')) {
exec("composer require yangze/spiderx");
}
include_once __DIR__ . '/vendor/autoload.php';
$config = [
'name' => 'sina',
'tasknum' => 1,
'start' => [
'http://roll.news.sina.com.cn/news/gnxw/gdxw1/index.shtml',
],
'rule' => [
[
'name' => 'list',
'type' => 'list',
'url' => '#gdxw1/index_\d+.shtml#',
'data' => [
'title' => function ($pageInfo, $html, $data) {
preg_match_all('/<li><a href=".*?" target="_blank">(.*?)<\/a><span>/i', $html, $matches);
return $matches[1];
}
]
],
]
];
$spider = new SpiderX\SpiderX($config);
$spider->on_fetch_list = function ($pageInfo, $html, $data) {
file_put_contents(__DIR__ . '/data.txt', implode("\n", $data['title']) . "\n", FILE_APPEND | LOCK_EX);
};
$spider->start();
```
### 2、命令行执行(需要composer下载依赖,时间跟网速有关)
```php
php index.php run
```
## 配置说明
| 字段 | 类型 | 说明 |
|--------:|--------|--------|
|name | string | 任务名称,队列名称根据name值生成。如果要做分布式的,可以选择用相同的name值 |
|tasknum| int | 任务数量,默认为1|
|start | array |采集入口url|
|rule | array |采集规则,具体参考下方说明|
### rule值说明
rule值为数组形式,每个二级元素为一个单元。
| 字段 | 类型 | 说明 |
|--------|--------|--------|
|name| string | 任务名称,队列名称根据name值生成。如果要做分布式的,可以选择用相同的name值 |
|name| string | 页面类型,选项为list 或者detail |
|url| string |入口url,有2种形式,一种是'#article_\d+#'这样的正则,从各个页面中抓取;一种是取其他单元的name值与所取单元的data值组合,比如一个单元name为news_list,data中有个元素为url,则组合成news_list.url赋值给当前字段|
|data| array |要采集的数据,以回调方式赋值,形式为:`key => function ($pageInfo, $html, $data) { return '';}`|
## 回调说明
### 通用回调方法
开始任务
```php
on_start = function() use($spiderx) {
// 可以在此方法中添加用户登录,增加url队列操作
//$spiderx->addUrl([]);
}
```
任务完成
```php
on_finish = function() {
// 任务执行完成,可以发送通知,导入数据库,删除日志文件等
}
```
向队列中添加url数据
```php
on_add_url = function($pageInfo) {
// 如果调转当前回调,需要返回true,才会向队列中添加数据
}
```
重试,url请求失败,重新请求,默认为3次
```php
on_retry_page = function($pageInfo) {
//返回true表示需要重试
}
```
如果获取不到html数据,可以重写setGetHtml方法
```php
setGetHtml = function($pageInfo) {
return file_get_contents($pageInfo['url']);
}
```
类似的还有setGetLinks方法,抽取页面中的链接,或者其他url存储方式
```php
setGetLinks = function($html) {
}
```
### 页面加载回调
需要依赖用户设置的每个rule下面单元的name值。假设我们设置的name值为news. 则对应的回调方法有:
请求url前回调
```php
on_loadding_{news,需要替换不同的name值} = function($pageInfo) {
// pageInfo 为当前页面的相关信息
//返回true表示需要请求这个页面
}
```
获取html后回调
```php
on_loaded_{news,需要替换不同的name值} = function($pageInfo, $html) {
//html表示当前的html数据
}
```
解析页面数据后回调,一般用于保存数据
```php
on_fetch_{news,需要替换不同的name值} = function($pageInfo, $html, $data) {
//data值为解析的数据
}
```
## 高级玩法
### 获取页面数据
1. 字符串截取
```php
//字符串模式
\SpiderX\Lib\Util::subStrByStr($start, $end, $html, true);
//正则模型
\SpiderX\Lib\Util::subStrByPreg($start, $end, $html, true);
```
2. xpath
```php
$crawler = new \Symfony\Component\DomCrawler\Crawler();
$crawler->addHtmlContent($html);
$crawler->filterXPath('//h3')
$crawler->filterXPath('//h3')->text();
$crawler->filterXPath('//h3')->nodeName();
$crawler->filterXPath('//h3')->attr('class');
$attributes = $crawler
->filterXpath('//body/p')
->extract(array('_text', 'class'));
$crawler->filterXPath('//*[@id="YKTabCon2_10"]//tr')->each(function ($node, $i){
//
});
```
3. 正则
```php
preg_match();
```
### 设置cookie和header头
> 需要重写`setGetHtml`方法
```php
$spider->setGetHtml = function ($pageInfo) {
$pageInfo['cookie'] = '...';
$pageInfo['extra'] = [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
'Referer' => '...',
]
];
return Url::getHtml($pageInfo);
};
```
### 模拟登录
需要要`on_start`回调中添加自动登录逻辑
> 部分页面可能需要先get方式获取页面中的参数,然后再发起POST请求
```php
$spider->on_start = function () use ($spider) {
$pageInfo = [
'type' => 'index',
'name' => 'login',
'method' => 'post', // 发送post提交
'url' => 'http://127.0.0.1:3200/index.php/action/login?_=b0fd8734e0687a6cfe352e3f0fcbc5f6',
'query' => [
'name' => 'admin',
'password' => 'admin',
'referer' => 'http://127.0.0.1:3200/admin/',
], // 请求参数
'cookie' => true, // 需要共享cookie
'extra' => [
'headers' => [
'User-Agent' => 'testing/1.0',
'Accept' => 'application/json',
]
]// 添加额外参数,参考
];
$spider->addUrl($pageInfo);
return true;
};
```
### 无限级数据采集
实现的方式就是在data的单元中,把url的值设置为上一个单元的`name.DataField`的形式
> 参考demo目录sina文件。
### post提交表单,post分页抓取数据
实现方式为自定义添加url队列,请求类型method为post,请求参数query为数组或者字符串形式:
```php
$spider->addUrl([
'type' => 'detail', // 保持和单元的name,type一致
'name' => 'detail',
'url' => 'http://smeimdf.mofcom.gov.cn/news/searchEntpAudit.jsp',
'method' => 'post', // 请求方式
'query' => [ // 请求参数
'fund_type' => $fund_type,
'province' => 340000,
],
'context' => [ // 上下文数据,可以很方便的在多任务中传数据
'fund_type' => $fund_type,
'province' => '-',
'province_name' => '-',
]
]);
```
### 快速导出列表数据和表格数据
```php
$dataList = (new \SpiderX\Lib\UtilXpath)->setAttr(['title'])->setHtml($html)->setRange('//table[@id="YKTabCon2_10"]')->getResult();
foreach($dataList as $data) {
array_walk($data, function (&$item) {
$item = str_ireplace(',', ',', $item);
$item = trim($item);
});
file_put_contents('data.csv', implode(',', $data) . "\n", FILE_APPEND | LOCK_EX);
}
```
## 执行效果
```shell
Usage:
demo/sina.php {command} [--opt -v -h ...] [arg0 arg1 arg2=value2 ...]
Options:
--debug Setting the application runtime debug level
--profile Display timing and memory usage information
--no-color Disable color/ANSI for message out
没有合适的资源?快使用搜索试试~ 我知道了~
PHP 爬虫 - 多线程,可定制爬虫框架 .zip
共132个文件
gif:75个
js:21个
php:19个
0 下载量 153 浏览量
2024-02-05
16:13:37
上传
评论
收藏 908KB ZIP 举报
温馨提示
爬虫 PHP 爬虫 - 多线程,可定制爬虫框架 .zip
资源推荐
资源详情
资源评论
收起资源包目录
PHP 爬虫 - 多线程,可定制爬虫框架 .zip (132个子文件)
layui.css 68KB
layer.css 14KB
layui.mobile.css 10KB
laydate.css 7KB
code.css 1KB
iconfont.eot 40KB
59.gif 10KB
22.gif 10KB
24.gif 8KB
13.gif 7KB
16.gif 7KB
39.gif 6KB
64.gif 6KB
63.gif 6KB
50.gif 6KB
loading-0.gif 6KB
4.gif 6KB
1.gif 5KB
42.gif 5KB
71.gif 5KB
21.gif 5KB
20.gif 5KB
29.gif 5KB
70.gif 4KB
5.gif 4KB
17.gif 4KB
27.gif 4KB
9.gif 4KB
44.gif 4KB
11.gif 4KB
8.gif 4KB
3.gif 4KB
23.gif 4KB
34.gif 4KB
41.gif 4KB
38.gif 4KB
65.gif 3KB
32.gif 3KB
45.gif 3KB
7.gif 3KB
12.gif 3KB
26.gif 3KB
60.gif 3KB
2.gif 3KB
40.gif 3KB
25.gif 3KB
19.gif 3KB
66.gif 3KB
18.gif 3KB
46.gif 3KB
10.gif 3KB
28.gif 3KB
51.gif 3KB
57.gif 3KB
67.gif 3KB
0.gif 3KB
48.gif 3KB
43.gif 3KB
30.gif 2KB
61.gif 2KB
33.gif 2KB
69.gif 2KB
14.gif 2KB
47.gif 2KB
36.gif 2KB
49.gif 2KB
58.gif 2KB
6.gif 2KB
54.gif 2KB
53.gif 2KB
56.gif 2KB
62.gif 2KB
31.gif 2KB
55.gif 2KB
35.gif 2KB
15.gif 2KB
loading-2.gif 2KB
37.gif 1KB
68.gif 1KB
52.gif 777B
loading-1.gif 701B
.gitignore 31B
index.html 14KB
command.jpg 182KB
spiderx.jpg 61KB
layui.all.js 254KB
jquery.js 95KB
mobile.js 33KB
table.js 30KB
laydate.js 27KB
layer.js 22KB
layedit.js 12KB
colorpicker.js 11KB
form.js 9KB
element.js 7KB
upload.js 7KB
slider.js 7KB
layui.js 7KB
laypage.js 4KB
carousel.js 4KB
共 132 条
- 1
- 2
资源评论
张无忌打怪兽
- 粉丝: 2046
- 资源: 1197
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功