2017 © Pedro Peláez
 

library lcrawl

image

lndj/lcrawl

  • Monday, March 5, 2018
  • by lndj
  • Repository
  • 14 Watchers
  • 102 Stars
  • 53 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 49 Forks
  • 3 Open issues
  • 10 Versions
  • 8 % Grown

The README.md

简介

Join the chat at https://gitter.im/lndj/Lcrawl, (*1)

使用PHP实现正方教务系统爬虫功能。, (*2)

可能是正方教务系统最优雅的一只爬虫。, (*3)

安装

使用 composer 进行安装: composer require lndj/lcrawl, (*4)

要体验最新功能,可以执行:, (*5)

git clone https://github.com/lndj/Lcrawl.git
cd Lcrawl
composer install

注意:请先安装 composer, (*6)

Example

<?php

 // Require the composer autoload file
require './vendor/autoload.php';

$stu_id = '201201148';
//Notice: This is NOT test account!!!
$password = 'xxxxxxxx';

$user = ['stu_id' => $stu_id, 'stu_pwd' => $password];

$client = new Lcrawl('http://xuanke.lzjtu.edu.cn/', $user);

//登陆 -- 没有开启会话缓存,必须调用登陆方法。
$client->login();

//获取所有数据
$all = $client->setUa('Lcrawl Spider V2.0.2')->getAll();


// $client->getSchedule();
// $client->getCet();


在请求过程中,你还可以设置 Referer/Timeoutheader 信息,直接采用链式调用即可。, (*7)

会话缓存

在请求过程中,可以启用会话缓存功能,可以有效减少教务系统会话开启数量。, (*8)

//实例化过程中传入第三个值
$client = new Lcrawl('http://xuanke.lzjtu.edu.cn/', $user, true);

$all = $client->setUa('Lcrawl Spider V2.0.2')->setTimeOut(3.0)->getAll();

高级用法

为达到在登陆一次后的一段时间内,不需要再次执行登陆操作便可直接获取数据,减少教务网请求量,可以使用会话缓存。, (*9)

首先,在实例化Lcrawl时,传入第三个参数为 true 。 例如:, (*10)

//实例化过程中传入第三个值
$client = new Lcrawl('http://xuanke.lzjtu.edu.cn/', $user, true);

第三个参数即表示开启会话缓存。, (*11)

本项目使用 doctrine/cache 来完成缓存工作,它支持基本目前所有的缓存引擎。, (*12)

在我们的 Lcrawl 中的所有缓存默认使用文件缓存,缓存路径取决于PHP的临时目录,如果你需要自定义缓存,那么你需要做如下的事情:, (*13)


use Doctrine\Common\Cache\RedisCache; $cacheDriver = new RedisCache(); // 创建 redis 实例 $redis = new Redis(); $redis->connect('redis_host', 6379); $cacheDriver->setRedis($redis); //设置使用redis来缓存会话 $client->setCache($cacheDriver);

你可以参考doctrine/cache官方文档来替换掉应用中默认的缓存配置:, (*14)

以 redis 为例, (*15)

请先安装 redis 拓展:https://github.com/phpredis/phpredis, (*16)

设置登录过程参数

本SDK默认使用的是 ysdx_default.aspx 为登陆的 uri 。若要使用其他 uri ,可自行抓包获取登录过程的参数。, (*17)

在实例化时,传入第四个参数, $loginParam ,数组的key为固定值,value传入抓包过程中 POST body 的字段名即可。, (*18)


$loginParam = [ 'viewstate' => '__VIEWSTATE', //隐藏域字段名称 'stu_id' => 'TextBox1', //学号字段名称 'passwod' => 'TextBox2', //密码字段 'role' => 'RadioButtonList1', //角色 'button' => 'Button1' //按钮 ]; $client = new Lcrawl('http://xuanke.lzjtu.edu.cn/', $user, true, $loginParam); //other code...

API

getAll()

获取所有数据,并发获取, (*19)

getSchedule()

获取课表数据, (*20)

getGrade()

获取成绩数据, (*21)

getCet()

获取四六级数据, (*22)

getExam()

获取考试安排数据, (*23)

setX()

设置相应 Header 值,例如: setUa()/setTimeout()/setReferer() 等。, (*24)

getX()

Getter., (*25)

Laravel 中使用

在 Laravel 中框架使用 predis/predis ,那么我们就得使用 Doctrine\Common\Cache\PredisCache:, (*26)


use Doctrine\Common\Cache\PredisCache; $predis = app('redis')->connection();// connection($name), $name 默认为 `default` $cacheDriver = new PredisCache($predis); //设置使用redis来缓存会话 $client->setCache($cacheDriver);

上面提到的 app('redis')->connection($name) , 这里的 $namelaravel项目中配置文件 database.phpredis 配置名 defaulthttps://github.com/laravel/laravel/blob/master/config/database.php#L118 如果你使用的其它连接,对应传名称就好了。, (*27)

License

MIT License, (*28)

The Versions

05/03 2018

v1.0

1.0.0.0

  Sources   Download

26/02 2017

dev-dev

dev-dev http://lcrawl.lzjtuhand.com

一只优雅的正方教务系统爬虫。

  Sources   Download

MIT

The Requires

 

wechat 正方 模拟登陆 教务系统

30/10 2016

dev-master

9999999-dev http://lcrawl.lzjtuhand.com

一只优雅的正方教务系统爬虫。

  Sources   Download

MIT

The Requires

 

wechat 正方 模拟登陆 教务系统

30/10 2016

v3.1.0

3.1.0.0 http://lcrawl.lzjtuhand.com

一只优雅的正方教务系统爬虫。

  Sources   Download

MIT

The Requires

 

wechat 正方 模拟登陆 教务系统

16/09 2016

v3.0.1

3.0.1.0 http://lcrawl.lzjtuhand.com

一只优雅的正方教务系统爬虫。

  Sources   Download

MIT

The Requires

 

wechat 正方 模拟登陆 教务系统

26/05 2016

v3.0.0

3.0.0.0 http://lcrawl.lzjtuhand.com

一只优雅的正方教务系统爬虫。

  Sources   Download

MIT

The Requires

 

wechat 正方 模拟登陆 教务系统

21/05 2016

v2.0.1

2.0.1.0 http://lcrawl.lzjtuhand.com

一只优雅的正方教务系统爬虫。

  Sources   Download

MIT

The Requires

 

wechat 正方 模拟登陆 教务系统

11/05 2016

v2.0.0

2.0.0.0 http://lcrawl.lzjtuhand.com

一只优雅的正方教务系统爬虫。

  Sources   Download

MIT

The Requires

 

wechat 正方 模拟登陆 教务系统