forked from woondroo/curl_multi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplider.php
More file actions
executable file
·207 lines (172 loc) · 5.39 KB
/
splider.php
File metadata and controls
executable file
·207 lines (172 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* PHP通用多线程采集
*
* 2013-06-28 woondroo create
*/
header("Content-type: text/html;charset=utf-8");
set_time_limit(0);
require("libs/class_curl_multi.php");
//连接数据库
$link = mysql_connect("localhost","root","greenwen");
mysql_select_db("www_curlmulti",$link);
//清空数据库
mysql_query("TRUNCATE TABLE content");
//域名前缀
$base = "http://sellbest.net";
//需要采集的规则列表(分页)
$list = array(
'http://sellbest.net/by-category/page[1-2]/36-iPad-CASES.html'
);
//在列表页面内容链接表达式
$list_rules = '<p class="productName">.*?<a href="(.*?)">.*?</a>.*?</p>';
//内容页面信息字段表达式
$detail_rules = array(
'meta_title'=>'<title>(.*?)</title>',
'meta_keywords'=>'<meta name="keywords" content="(.*?)" />',
'meta_description'=>'<meta name="description" content="(.*?)" />',
'product_name'=>'<h4 class="h4-title float-l"> (.*?)</h4>',
'product_image'=>'<div class="v-inner">.*?<a href="(.*?)" id="originalImg"><img src=".*?" alt=".*?" /></a>.*?</div>',
'product_price'=>'Our Price : <strong>(.*?)</strong>',
'product_description'=>'<div class="description-text" id="description"><div class="border-cont">(.*?)</div>',
);
//实例
$mp = new MultiHttpRequest();
//调试使用记录采集条目
$j = 1;
//每次并发几个链接
$limit = 10;
// 分页时被跳过的页数
$last_page = 0;
//开始采集
foreach ($list as $link) {
//解析列表页数
preg_match_all('/\[(.*)\]/i',$link,$_page);
if($_page[1][0]==''){
continue;
}
$pages = explode('-',$_page[1][0]);
if(count($pages) != 2){
continue;
}
$urls = array();
for($i=$pages[0];$i<=$pages[1] || $last_page === 1;$i++){
if(count($urls) < $limit && $last_page === 0){
$urls[] = preg_replace('/\[(.*)\]/i',$i,$link);
if($i < $pages[1]){
continue;
}
}
// var_dump($urls);echo '<br/>';
$last_page = 0;
//采集列表内容
$mp->set_urls($urls);
$contents = $mp->start();
foreach ($contents as $content) {
$content = _prefilter($content);
//debug
// exit($content);
//匹配内容
preg_match_all('/'.str_replace('/', '\/', addslashes($list_rules)).'/i',$content,$pregArr);
$detail_urls = array();
foreach($pregArr[1] as $detail_key=>$detail_value){
$data = array();
if(count($detail_urls) < $limit ){
$content_url = $base.$detail_value;
$detail_urls[$content_url] = $content_url;
if($pregArr[1][$detail_key+1] != ''){
continue;
}
}
// var_dump($detail_urls);echo '<br/>';
//continue;
$mp->set_urls($detail_urls);
$details = $mp->start();
//图片路径临时存放
$images_urls = array();
//采集内容页面
foreach ($details as $url_key=>$detail) {
$detail = _prefilter($detail);
//debug
// exit($detail);
foreach ($detail_rules as $key => $value) {
preg_match_all('/'.str_replace('/', '\/', addslashes($value)).'/i',$detail,$detailArr);
//处理特殊这段信息
switch ($key) {
case 'product_image':
$data[$key] = "images/".md5($detailArr[1][0]).".jpg";
if(!file_exists($data[$key])){
$images_urls[$data[$key]] = $base.$detailArr[1][0];
}
break;
case 'product_description':
$data[$key] = trim(strip_tags($detailArr[1][0]));
break;
default:
$data[$key] = $detailArr[1][0];
break;
}
}
//产品url
$data['product_url'] = $url_key;
//转义采集后的数据
foreach ($data as $_k => $_v) {
$data[$_k] = addslashes($_v);
}
//入库
$r = mysql_query("
insert into `content` values(
null,
'{$data['meta_title']}',
'{$data['meta_keywords']}',
'{$data['meta_description']}',
'{$data['product_name']}',
'{$data['product_image']}',
'{$data['product_price']}',
'{$data['product_description']}',
'{$data['product_url']}')");
//打印log
_flush($j++."|".$r."|".$data['product_name']."<br/>");
//_flush($data);
}
//远程图片本地化
$mp->set_urls($images_urls);
$images = $mp->start();
foreach ((array)$images as $image_key => $image_value) {
if (!empty($image_key)) {
_flush("store image:".$image_key."<br/>");
file_put_contents($image_key,$image_value);
}
}
//清空内容url并加入本次循环url。不然本次会被跳过
$content_url = $base.$detail_value;
$detail_urls = array($content_url=>$content_url);
}
}
//清空内容url并加入本次循环url。不然本次会被跳过
if ($i == $pages[1] && ($pages[1] - $i) % $limit > 0) {
$last_page = 1;
}
$urls = array(preg_replace('/\[(.*)\]/i',$i,$link));
}
}
// 输出日志
function _flush($msg) {
print_r ($msg);
ob_flush();
flush();
}
// 对抓去到的内容做简单过滤(过滤空白字符,便于正则匹配)
function _prefilter($output) {
$output=preg_replace("/\/\/[\S\f\t\v ]*?;[\r|\n]/", "", $output);
$output=preg_replace("/\<\!\-\-[\s\S]*?\-\-\>/", "", $output);
$output=preg_replace("/\>[\s]+\</", "><", $output);
$output=preg_replace("/;[\s]+/", ";", $output);
$output=preg_replace("/[\s]+\}/", "}", $output);
$output=preg_replace("/}[\s]+/", "}", $output);
$output=preg_replace("/\{[\s]+/", "{", $output);
$output=preg_replace("/([\s]){2,}/", "$1", $output);
$output=preg_replace("/[\s]+\=[\s]+/", "=", $output);
return $output;
}
?>