Skip to content

Commit afd3ab3

Browse files
JinYi ChenJinYi Chen
authored andcommitted
Update PublicCore ,Add Bing Spider,Update Spider Load,Update .gitignore
1 parent aaaec0e commit afd3ab3

File tree

8 files changed

+100
-34
lines changed

8 files changed

+100
-34
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Api/
2+
Api/*
23
Resource/
4+
Resource/*
35
.DS_Store/
6+
.DS_Store
47
.idea/

Config.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,13 @@
1010
define("FILE_PATH","Resource"); //存放文件文件夹名
1111

1212
//网站API链接
13-
define("PIXABAY_API_URL","https://pixabay.com/api/");
13+
define("PIXABAY_API_URL","https://pixabay.com/api/");
14+
15+
//是否记录图片链接到数据库
16+
define("SAVE_IMAGES_URL_DATABASE","false");
17+
18+
//数据库连接
19+
define("DATABASE_URL","localhost");
20+
define("DATABASE_USERNAME","root");
21+
define("DATABASE_PASSWORD","");
22+
define("DATABASE_DATABASE","images");

Spider.php

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,14 @@
55
* Date: 2018/7/31
66
* Time: 下午9:01
77
*/
8-
9-
require_once "src/PublicCore.php";
8+
//引用文件
109
require_once "Config.php";
10+
require_once "Src/DbCore.php";
11+
require_once "Src/PublicCore.php";
1112

1213
//遍历目录文件
13-
function print_dir($dir_path)
14-
{
15-
$files = array();
16-
if (@$handle = opendir($dir_path)) { //注意这里要加一个@,不然会有warning错误提示:)
17-
while (($file = readdir($handle)) !== false) {
18-
if ($file != ".." && $file != ".") { //排除根目录;
19-
$files[] = $file;
20-
}
21-
}
22-
closedir($handle);
23-
return $files;
24-
}
25-
}
26-
27-
$dir = print_dir('src' . DIRECTORY_SEPARATOR . 'spider');
28-
14+
$spiderCore = new PublicCore();
15+
$dir = $spiderCore->print_dir('Src' . DIRECTORY_SEPARATOR . 'Spider');
2916

3017
//输出可以选择的爬虫
3118
$spider = "";
@@ -34,17 +21,21 @@ function print_dir($dir_path)
3421
}
3522
$print = "
3623
=============================
37-
PHP Images Spider".
38-
PHP_EOL
39-
. $spider .
40-
PHP_EOL.
41-
"Chenjinyi:https://github.com/Chenjinyi
24+
PHP Images Spider"
25+
. $spiderCore->eol($spider).
26+
"Chenjinyi:https://github.com/Chenjinyi
4227
=============================
43-
" . PHP_EOL . "请输入你选择的爬虫:";
28+
" . PHP_EOL .
29+
"请输入你选择的爬虫:";
4430
print_r($print);
4531
$input = trim(fgets(STDIN));
4632

33+
$t1 = microtime(true);//记录运行时间
4734

4835
//使用爬虫
49-
$spider_path = 'src' . DIRECTORY_SEPARATOR . 'spider/';
50-
empty($dir[$input]) ? die('参数错误') : include_once $spider_path . $dir[$input];
36+
$spider_path = 'Src' . DIRECTORY_SEPARATOR . 'Spider/';
37+
empty($dir[$input]) ? die(PHP_EOL . '参数错误') : include_once $spider_path . $dir[$input];
38+
39+
$t2 = microtime(true);//记录运行结束时间
40+
41+
print_r(PHP_EOL . '耗时' . round($t2 - $t1, 3) . "");//输入运行时间

Src/DbCore.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jinyi
5+
* Date: 2018/8/1
6+
* Time: 上午11:00
7+
*/
8+
9+
class DbCore
10+
{
11+
12+
}

src/PublicCore.php renamed to Src/PublicCore.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PublicCore
1414
* @param $url string 请求URL
1515
* @return mixed 返回获取信息
1616
*/
17-
public function curl_get($url,$user_agent)
17+
public function curl_get($url, $user_agent)
1818
{
1919
$ch = curl_init(); //初始化一个cURL会话
2020
curl_setopt($ch, CURLOPT_URL, $url);//设置需要获取的 URL 地址
@@ -52,6 +52,7 @@ public function image_save($file_url, $dir_name)
5252
}
5353
}
5454
}
55+
5556
/**
5657
* 创建文件夹
5758
* @param $dir_name string 文件夹名
@@ -110,14 +111,35 @@ public function print_dir($dir_path)
110111
*/
111112
public function check_api_file($filename)
112113
{
113-
$file_path=API_PATH.DIRECTORY_SEPARATOR.$filename;
114-
if (!file_exists($file_path)){
114+
$file_path = API_PATH . DIRECTORY_SEPARATOR . $filename;
115+
if (!file_exists($file_path)) {
115116
touch($file_path);
116117
}
117-
$file=file_get_contents($file_path);
118-
if (!empty($file)){
118+
$file = file_get_contents($file_path);
119+
if (!empty($file)) {
119120
return $file;
120121
}
121122
return false;
122123
}
124+
125+
126+
/**
127+
* 换行(没什么用的function)
128+
* @param $string
129+
* @return string
130+
*/
131+
public function eol($string)
132+
{
133+
return PHP_EOL.$string.PHP_EOL;
134+
}
135+
136+
/**
137+
* 获取文件夹内拥有多少个文件
138+
* @param $dir
139+
* @return int
140+
*/
141+
public function images_number($dir){
142+
return count($this->print_dir($dir));
143+
144+
}
123145
}

Src/Spider/Artstation.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jinyi
5+
* Date: 2018/8/1
6+
* Time: 上午10:11
7+
*/
8+
9+
class Artstation
10+
{
11+
12+
}

Src/Spider/Bing.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jinyi
5+
* Date: 2018/8/1
6+
* Time: 上午10:12
7+
*/
8+
9+
class Bing
10+
{
11+
12+
}
13+
14+
$ch = curl_init();curl_setopt($ch,CURLOPT_URL,"http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1");
15+
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//抓取
16+
$result = curl_exec($ch);$result = json_decode($result,true);//链接
17+
$save_url = "http://www.bing.com".$result["images"][0]['url'];$save = file_get_contents($save_url);
18+
file_put_contents('Resource'.DIRECTORY_SEPARATOR."Bing-".$result["images"][0]["startdate"].".jpg",$save);//下载

src/spider/PixabayCore.php renamed to Src/Spider/Pixabay.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Time: 下午1:10
77
*/
88

9-
class PixabayCore
9+
class Pixabay
1010
{
1111

1212
}
@@ -21,7 +21,6 @@ class PixabayCore
2121
"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"
2222
);
2323
//获取数据
24-
$spiderCore = new PublicCore();
2524
$spiderCore->init_dir();
2625
$key = $spiderCore->check_api_file('PixabayApiKey') ?: die("PixabayKey为空");
2726
$q = $spiderCore->user_input("请输入一个需要查询的字符串");

0 commit comments

Comments
 (0)