Skip to content

Commit 78191fa

Browse files
yangzetaoyangzetao
authored andcommitted
修改set_proxies 为 proxy,支持传入代理字符串和数组
1 parent 935f896 commit 78191fa

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

core/phpspider.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class phpspider
3838
* 版本号
3939
* @var string
4040
*/
41-
const VERSION = '2.1.0';
41+
const VERSION = '2.1.1';
4242

4343
/**
4444
* 爬虫爬取每个网页的时间间隔,0表示不延时, 单位: 毫秒
@@ -156,7 +156,7 @@ class phpspider
156156
'headers' => array(), // 此url的Headers, 可以为空
157157
'params' => array(), // 发送请求时需添加的参数, 可以为空
158158
'context_data'=> '', // 此url附加的数据, 可以为空
159-
'proxies' => false, // 是否使用代理
159+
'proxy' => false, // 是否使用代理
160160
'try_num' => 0 // 抓取次数
161161
'max_try' => 0 // 允许抓取失败次数
162162
)
@@ -361,7 +361,7 @@ function __construct($configs = array())
361361
}
362362

363363
$configs['name'] = isset($configs['name']) ? $configs['name'] : 'phpspider';
364-
$configs['proxies'] = isset($configs['proxies']) ? $configs['proxies'] : array();
364+
$configs['proxy'] = isset($configs['proxy']) ? $configs['proxy'] : false;
365365
$configs['user_agent'] = isset($configs['user_agent']) ? $configs['user_agent'] : self::AGENT_PC;
366366
$configs['client_ip'] = isset($configs['client_ip']) ? $configs['client_ip'] : array();
367367
$configs['interval'] = isset($configs['interval']) ? $configs['interval'] : self::INTERVAL;
@@ -1233,9 +1233,9 @@ public function request_url($url, $link = array())
12331233
}
12341234

12351235
// 是否设置了代理
1236-
if (!empty($link['proxies']))
1236+
if ($link['proxy'])
12371237
{
1238-
requests::set_proxies($link['proxies']);
1238+
requests::set_proxy($link['proxy']);
12391239
}
12401240

12411241
// 如何设置了 HTTP Headers
@@ -1610,9 +1610,9 @@ public function link_compress($link)
16101610
unset($link['context_data']);
16111611
}
16121612

1613-
if (empty($link['proxies']))
1613+
if (empty($link['proxy']))
16141614
{
1615-
unset($link['proxies']);
1615+
unset($link['proxy']);
16161616
}
16171617

16181618
if (empty($link['try_num']))
@@ -1651,7 +1651,7 @@ public function link_uncompress($link)
16511651
'headers' => isset($link['headers']) ? $link['headers'] : array(),
16521652
'params' => isset($link['params']) ? $link['params'] : array(),
16531653
'context_data' => isset($link['context_data']) ? $link['context_data'] : '',
1654-
'proxies' => isset($link['proxies']) ? $link['proxies'] : self::$configs['proxies'],
1654+
'proxy' => isset($link['proxy']) ? $link['proxy'] : self::$configs['proxy'],
16551655
'try_num' => isset($link['try_num']) ? $link['try_num'] : 0,
16561656
'max_try' => isset($link['max_try']) ? $link['max_try'] : self::$configs['max_try'],
16571657
'depth' => isset($link['depth']) ? $link['depth'] : 0,

core/requests.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ public static function set_timeout($timeout)
8282
* @author seatle <seatle@foxmail.com>
8383
* @created time :2016-09-18 10:17
8484
*/
85-
public static function set_proxies($proxies)
85+
public static function set_proxy($proxy)
8686
{
87-
self::$proxies = $proxies;
87+
self::$proxies = is_array($proxy) ? $proxy : array($proxy);
8888
}
8989

9090
/**

core/selector.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ private static function _xpath_select($html, $selector, $remove = false)
177177
private static function _css_select($html, $selector, $remove = false)
178178
{
179179
$selector = self::css_to_xpath($selector);
180+
//echo $selector."\n";
181+
//exit("\n");
180182
return self::_xpath_select($html, $selector, $remove);
181183
// 如果加载的不是之前的HTML内容,替换一下验证标识
182184
//if (self::$dom_auth['css'] != md5($html))
@@ -271,6 +273,7 @@ public static function css_to_xpath($selectors)
271273
{
272274
$xquery .= '*';
273275
}
276+
// ID用精确查询
274277
$xquery .= "[@id='".substr($s, 1)."']";
275278
}
276279
// CLASSES
@@ -280,7 +283,8 @@ public static function css_to_xpath($selectors)
280283
{
281284
$xquery .= '*';
282285
}
283-
$xquery .= '[@class]';
286+
// CLASS用模糊查询
287+
$xquery .= "[contains(@class,'".substr($s, 1)."')]";
284288
}
285289
// ATTRIBUTES
286290
else if ($s[0] == '[')

0 commit comments

Comments
 (0)