Skip to content

Commit d26a593

Browse files
committed
❤️ 添加注释 & 更改Bilibili菜单输出
1 parent 3c47777 commit d26a593

File tree

7 files changed

+70
-53
lines changed

7 files changed

+70
-53
lines changed

Spider.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@
3838
`=--=-'
3939
4040
\e[0m
41-
====================================================
41+
" . $spiderCore->splitLine . "
4242
\033[33m PHP Images Spider \033[0m" .
4343
"\033[34m" . $spiderCore->eol($spider) . "\033[0m" .
4444
"
4545
\033[33m Chenjinyi:https://github.com/Chenjinyi \033[0m
46-
====================================================
47-
" . PHP_EOL .
46+
" . $spiderCore->splitLine . PHP_EOL .
4847
"请输入你选择的爬虫: ";
4948
print_r($print);
5049
$input = trim(fgets(STDIN));

Src/PublicCore.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,22 @@ public function print_menu(array $spider)
251251
}
252252
return $result;
253253
}
254+
255+
/**
256+
* 原本Bilibili使用的菜单输出
257+
* print_menu的二次封装
258+
* @param $string
259+
* @param $spiderName string
260+
*/
261+
public function bMenu($string, $spiderName)
262+
{
263+
print_r( //输出用户选择的菜单
264+
$this->splitLine. PHP_EOL . "
265+
\033[33m $spiderName Spider \033[0m" . PHP_EOL .
266+
$this->eol($this->print_menu($string)) .
267+
$this->splitLine. PHP_EOL
268+
);
269+
}
270+
271+
public $splitLine="<<<<<<<<<<<<<<<<<<================================>>>>>>>>>>>>>>>>>>";
254272
}

Src/Spider/Artstation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function index_spider_core($spiderCore, $spider_name, $parm)
6363
$result = $spiderCore->curl_get($url, $this->userAgent);
6464
$result = json_decode($result);
6565
$images_arr = $this->get_img_url($result, $spiderCore);
66-
$spiderCore->quick_down_img($this->spider_name . "-" . $spider_name, $images_arr,"Artstation");
66+
$spiderCore->quick_down_img($this->spider_name . "-" . $spider_name, $images_arr, "Artstation");
6767
$spiderCore->spider_wait(ARTSTATION_SLEEP, ARTSTATION_SLEEP_TIME_MIN, ARTSTATION_SLEEP_TIME_MAX);
6868
}
6969

@@ -83,7 +83,7 @@ public function search_core($spiderCore, $spider_name, $parm)
8383
$result = $spiderCore->curl_get($url, $this->userAgent);
8484
$result = json_decode($result);
8585
$images_arr = $this->get_img_url($result, $spiderCore);
86-
$spiderCore->quick_down_img($this->spider_name . "-" . $spider_name, $images_arr,"Artstation");
86+
$spiderCore->quick_down_img($this->spider_name . "-" . $spider_name, $images_arr, "Artstation");
8787
$spiderCore->spider_wait(ARTSTATION_SLEEP, ARTSTATION_SLEEP_TIME_MIN, ARTSTATION_SLEEP_TIME_MAX);
8888
}
8989

@@ -100,7 +100,7 @@ public function user($spiderCore)
100100
$result = json_decode($result);
101101

102102
$images_arr = $this->get_img_url($result, $spiderCore);
103-
$spiderCore->quick_down_img($this->spider_name . "-" . $user, $images_arr,"Artstation",$user);
103+
$spiderCore->quick_down_img($this->spider_name . "-" . $user, $images_arr, "Artstation", $user);
104104
}
105105

106106
public function latest($spiderCore) //最新图片

Src/Spider/Bcy.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,35 @@
88

99
class Bcy
1010
{
11-
public $userAgent=[
11+
//https://bcy.net/coser/index/ajaxloadtoppost?p=3&type=week&date=
12+
/**
13+
* 针对半次元Tag的爬取
14+
* URL规律
15+
* https://bcy.net/circle/timeline/loadtag?since=0&grid_type=timeline&tag_id=399&sort=hot
16+
* sort排序方式 recent最新
17+
* tag_id 标签id Cos为399
18+
* since 最后一天图片的时间戳
19+
*/
20+
21+
public $userAgent = [
1222
"Host:bcy.net",
1323
"Connection: keep-alive",
1424
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
1525
"Upgrade-Insecure-Requests: 1",
1626
"DNT:1",
1727
"Accept-Language:zh-CN,zh;q=0.8",
1828
"User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"
19-
2029
];
30+
31+
public function serve()
32+
{
33+
$spider = [
34+
1=>"tags",
35+
];
36+
37+
}
2138
}
2239

23-
//https://bcy.net/coser/index/ajaxloadtoppost?p=3&type=week&date=
40+
41+
$bcy = new Bcy();
42+
$bcy->serve();

Src/Spider/Bilibili.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class Bilibili
1818
"User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"
1919
];//设置用户user-agent
2020

21+
public $spiderName= "Bilibili";
22+
2123
public $rank_type = [ //排行榜
2224
1 => 'day',
2325
2 => 'month',
@@ -147,32 +149,9 @@ public function getYesterday($date)
147149
return $yesterday;
148150
}
149151

150-
151-
/**
152-
* 输出选择菜单
153-
* @param $string
154-
* @param $spiderCore
155-
*/
156-
public function menu($string, $spiderCore)
157-
{
158-
print_r( //输出用户选择的菜单
159-
"============================================" . PHP_EOL . "
160-
\033[33m BIlibili Spider \033[0m" .
161-
$spiderCore->eol($spiderCore->print_menu($string)) .
162-
"============================================" . PHP_EOL
163-
);
164-
}
165-
166152
/**
167153
* Bilibili 搜索爬虫
168154
* @param $spiderCore
169-
* https://api.vc.bilibili.com/link_draw/v1/doc/detail?doc_id=925202 //内容详细
170-
* https://api.bilibili.com/x/web-interface/search/type?jsonp=jsonp&search_type=photo&highlight=1&keyword=2b&order=totalrank&category_id=1
171-
* https://api.bilibili.com/x/web-interface/search/type?jsonp=jsonp&search_type=photo&highlight=1&keyword=2b&order=totalrank&category_id=2 摄影
172-
* https://api.bilibili.com/x/web-interface/search/type?jsonp=jsonp&search_type=photo&highlight=1&keyword=2b&order=pubdate 最新
173-
* https://api.bilibili.com/x/web-interface/search/type?jsonp=jsonp&search_type=photo&highlight=1&keyword=2b&order=totalrank&category_id=2 默认
174-
* https://api.bilibili.com/x/web-interface/search/type?jsonp=jsonp&search_type=photo&highlight=1&keyword=2b&order=stow&category_id=1 最多收藏
175-
* https://api.bilibili.com/x/web-interface/search/type?jsonp=jsonp&search_type=photo&highlight=1&keyword=2b&order=stow&category_id=0 全部
176155
*/
177156
public function search($spiderCore)
178157
{
@@ -238,7 +217,7 @@ public function search($spiderCore)
238217
*/
239218
public function quick_input($spiderCore, $string, $array, $exit_string, $default)
240219
{
241-
$this->menu($array, $spiderCore);
220+
$spiderCore->bMenu($array,$this->spiderName);
242221
$input = $spiderCore->user_input($string, $default);
243222
if (@empty($array[$input])) {
244223
die($exit_string);
@@ -307,6 +286,6 @@ public function top50($spiderCore)
307286

308287
$bilibili = new Bilibili();
309288

310-
$bilibili->menu($bilibili->mode, $spiderCore);
289+
$spiderCore->bMenu($bilibili->mode,$bilibili->spiderName);
311290
$mode = $spiderCore->user_input(PHP_EOL . "请选择你需要使用的模式:", null);
312291
@empty($user_mode = $bilibili->mode[$mode]) ? die(PHP_EOL . '没有这个爬虫模式') : $bilibili->$user_mode($spiderCore); //调用爬虫,并传入公用function

Src/Spider/Bing.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
* Time: 上午10:12
99
*/
1010

11-
$ch = curl_init();curl_setopt($ch,CURLOPT_URL,"http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1");
12-
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//抓取
13-
$result = curl_exec($ch);$result = json_decode($result,true);//链接
14-
$save_url = "http://www.bing.com".$result["images"][0]['url'];$save = file_get_contents($save_url);
15-
file_put_contents('Resource'.DIRECTORY_SEPARATOR."Bing-".$result["images"][0]["startdate"].".jpg",$save);//下载
11+
$ch = curl_init();
12+
curl_setopt($ch, CURLOPT_URL, "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1");
13+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//抓取
14+
$result = curl_exec($ch);
15+
$result = json_decode($result, true);//链接
16+
$save_url = "http://www.bing.com" . $result["images"][0]['url'];
17+
$save = file_get_contents($save_url);
18+
file_put_contents('Resource' . DIRECTORY_SEPARATOR . "Bing-" . $result["images"][0]["startdate"] . ".jpg", $save);//下载

Src/Spider/Pixabay.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,29 @@ class Pixabay
1111
{
1212

1313
}
14+
1415
//user-agent
15-
$user_agent=array(
16+
$user_agent = array(
1617
"Host:pixabay.com",
1718
"Connection: keep-alive",
1819
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
1920
"Upgrade-Insecure-Requests: 1",
2021
"DNT:1",
2122
"Accept-Language:zh-CN,zh;q=0.8",
2223
"User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"
23-
);
24-
25-
;
24+
);;
2625

2726
//获取数据
28-
$key = $spiderCore->check_api_file('PixabayApiKey') ?: die("PixabayKey为空".PHP_EOL."请在/API文件夹里输入你的API KEY");//获取用户Key
29-
$q = $spiderCore->user_input("请输入一个需要查询的字符串(不输入就随缘了):",RAND_KEYWORD[mt_rand(0,count(RAND_KEYWORD)-1)]); //获取查询内容
30-
$per_page = $spiderCore->user_input($spiderCore->eol("每次最多尝试下载200张")."尝试爬取的图片数量(每页图片张数)".$spiderCore->eol("最终下载图片数量=(图片张数*多次执行的图片页数)")."请输一页的图片数量3~200(默认为30):",30); //获取查询内容
31-
$page = $spiderCore->user_input(PHP_EOL."请输入获取的图片页数(默认为1):",1); //获取查询内容
27+
$key = $spiderCore->check_api_file('PixabayApiKey') ?: die("PixabayKey为空" . PHP_EOL . "请在/API文件夹里输入你的API KEY");//获取用户Key
28+
$q = $spiderCore->user_input("请输入一个需要查询的字符串(不输入就随缘了):", RAND_KEYWORD[mt_rand(0, count(RAND_KEYWORD) - 1)]); //获取查询内容
29+
$per_page = $spiderCore->user_input($spiderCore->eol("每次最多尝试下载200张") . "尝试爬取的图片数量(每页图片张数)" . $spiderCore->eol("最终下载图片数量=(图片张数*多次执行的图片页数)") . "请输一页的图片数量3~200(默认为30):", 30); //获取查询内容
30+
$page = $spiderCore->user_input(PHP_EOL . "请输入获取的图片页数(默认为1):", 1); //获取查询内容
3231

33-
$result = json_decode($result = $spiderCore->curl_get(PIXABAY_API_URL."?key=".$key."&q=".$q."&per_page=".$per_page."&page=".$page,$user_agent));//通过Api得到数据
34-
$images_arr=[];
32+
$result = json_decode($result = $spiderCore->curl_get(PIXABAY_API_URL . "?key=" . $key . "&q=" . $q . "&per_page=" . $per_page . "&page=" . $page, $user_agent));//通过Api得到数据
33+
$images_arr = [];
3534
//获取图片下载链接以及名字
36-
foreach ($result->hits as $images){
37-
$format=explode('.',$images->largeImageURL);
38-
array_push($images_arr,["pixabay-".$images->id.".".$format['2']=>$images->largeImageURL]);
35+
foreach ($result->hits as $images) {
36+
$format = explode('.', $images->largeImageURL);
37+
array_push($images_arr, ["pixabay-" . $images->id . "." . $format['2'] => $images->largeImageURL]);
3938
}
40-
$spiderCore->quick_down_img("pixabay-".$q,$images_arr,"Pixabay");
39+
$spiderCore->quick_down_img("pixabay-" . $q, $images_arr, "Pixabay");

0 commit comments

Comments
 (0)