From 5a89a483a2dd05b5d3dcd341cbcfc9a43f1091e3 Mon Sep 17 00:00:00 2001 From: kaka Date: Mon, 16 Jul 2018 21:54:41 +0700 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dconfigs=E2=80=94=E2=80=94?= =?UTF-8?q?=E4=B9=8Bfield=20=E6=97=A0=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/phpspider.php | 1 + core/selector.php | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/core/phpspider.php b/core/phpspider.php index b253b97..bff6748 100755 --- a/core/phpspider.php +++ b/core/phpspider.php @@ -1831,6 +1831,7 @@ public function get_fields($confs, $html, $url, $page) // 没有设置抽取规则的类型 或者 设置为 xpath if (!isset($conf['selector_type']) || $conf['selector_type']=='xpath') { + // 如果找不到,返回的是false $values = $this->get_fields_xpath($html, $conf['selector'], $conf['name']); } elseif ($conf['selector_type']=='css') diff --git a/core/selector.php b/core/selector.php index 4d8d001..f17cff4 100644 --- a/core/selector.php +++ b/core/selector.php @@ -116,7 +116,9 @@ private static function _xpath_select($html, $selector, $remove = false) if ($elements === false) { self::$error = "the selector in the xpath(\"{$selector}\") syntax errors"; - return false; + // 不应该返回false,因为isset(false)为true,更不能通过 !$values 去判断,因为!0为true,所以这里只能返回null + //return false; + return null; } $result = array(); @@ -159,7 +161,7 @@ private static function _xpath_select($html, $selector, $remove = false) } if (empty($result)) { - return false; + return null; } // 如果只有一个元素就直接返回string,否则返回数组 return count($result) > 1 ? $result : $result[0]; @@ -210,14 +212,14 @@ private static function _regex_select($html, $selector, $remove = false) if(@preg_match_all($selector, $html, $out) === false) { self::$error = "the selector in the regex(\"{$selector}\") syntax errors"; - return false; + return null; } $count = count($out); $result = array(); // 一个都没有匹配到 if ($count == 0) { - return false; + return null; } // 只匹配一个,就是只有一个 () elseif ($count == 2) @@ -242,7 +244,7 @@ private static function _regex_select($html, $selector, $remove = false) } if (empty($result)) { - return false; + return null; } return count($result) > 1 ? $result : $result[0];