Skip to content

Commit 3a054bd

Browse files
JinYi ChenJinYi Chen
authored andcommitted
Artstation Index Spider
1 parent 4170565 commit 3a054bd

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

Src/Spider/Artstation.php

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,80 @@ class Artstation
1616
"DNT:1",
1717
"Accept-Language:zh-CN,zh;q=0.8",
1818
"User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36"
19-
];
19+
];//设置用户user-agent
2020

2121
public $mode = [
22-
'trending',
22+
'1'=>'trending',
2323
'latest',
2424
'picks',
2525
'user',
26-
];
26+
];//可以选择的模式/可以调用的function
2727

2828
public $spider_name = "Artstation";
2929

30-
public function user($spiderCore)
30+
/**
31+
* 获取图片链接
32+
* @param $result
33+
* @param $spiderCore
34+
* @return array 下载链接
35+
*/
36+
public function get_img_url($result, $spiderCore)
3137
{
32-
@empty($user = $spiderCore->user_input("请输入你要抓取的用户ID(https://www.artstation.com/xxxxx):", null)) ? die(PHP_EOL . "用户不能为空") : print_r(PHP_EOL . "开始获取图片" . PHP_EOL);
33-
$result = $spiderCore->curl_get("https://www.artstation.com/users/" . $user . "/projects.json", $this->userAgent); //爬取json
34-
$result = json_decode($result);
35-
3638
$images_arr = [];
37-
//获取图片下载地址
3839
foreach ($result->data as $value) {
3940
$images_url = $value->cover->medium_image_url;
4041
$images_url = str_replace("medium", "large", $images_url);//生成更高清的图片下载地址
41-
$file_name = $value->title . "-" . $value->cover_asset_id . "-" . $user;//生成图片名
42+
$file_name = $value->title . "-" . $value->cover_asset_id;//生成图片名
4243
$file_name = $spiderCore->image_url_format($images_url, $file_name);
4344
array_push($images_arr, [$file_name => $images_url]);
4445
}
46+
return $images_arr;
47+
}
48+
49+
public function index_spider_core($spiderCore,$spider_name)
50+
{
51+
$posts_num = $spiderCore->user_input("请输入爬取页数(1页=50个作品)/(默认为1):", 1);
52+
for ($start_num = 1; $start_num <= $posts_num; $start_num++) {
53+
$url = "https://www.artstation.com/projects.json?page=" . $start_num . "&sorting=" . $spider_name;
54+
$result = $spiderCore->curl_get($url, $this->userAgent);
55+
$result = json_decode($result);
56+
$images_arr = $this->get_img_url($result, $spiderCore);
57+
$spiderCore->quick_down_img($this->spider_name . "-" . $spider_name, $images_arr);
58+
59+
}
60+
}
61+
62+
/**
63+
* 指定用户爬取
64+
* @param $spiderCore
65+
*/
66+
public function user($spiderCore)
67+
{
68+
@empty($user = $spiderCore->user_input("请输入你要抓取的用户ID(https://www.artstation.com/xxxxx):", null)) ? die(PHP_EOL . "用户不能为空") : print_r(PHP_EOL . "开始获取图片" . PHP_EOL);
69+
$result = $spiderCore->curl_get("https://www.artstation.com/users/" . $user . "/projects.json", $this->userAgent); //爬取json
70+
$result = json_decode($result);
71+
72+
$images_arr = $this->get_img_url($result, $spiderCore);
4573
$spiderCore->quick_down_img($this->spider_name . "-" . $user, $images_arr);
4674
}
4775

76+
/**
77+
* 最新图片爬取
78+
* @param $spiderCore
79+
*/
4880
public function latest($spiderCore)
4981
{
50-
82+
$this->index_spider_core($spiderCore,'latest');
5183
}
5284

5385
public function picks($spiderCore)
5486
{
55-
87+
$this->index_spider_core($spiderCore,'picks');
5688
}
5789

5890
public function trending($spiderCore)
5991
{
60-
92+
$this->index_spider_core($spiderCore,'trending');
6193
}
6294

6395
public function search($spiderCore)

0 commit comments

Comments
 (0)