本文整理匯總了PHP中Finder::addSelector方法的典型用法代碼示例。如果您正苦於以下問題:PHP Finder::addSelector方法的具體用法?PHP Finder::addSelector怎麽用?PHP Finder::addSelector使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Finder
的用法示例。
在下文中一共展示了Finder::addSelector方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: getContents
/**
* 采集內容
* @return void
*/
public function getContents()
{
/* 檢查初始化狀態 */
$this->checkIsInited();
/* 所有入口鏈接 */
foreach ($this->urls as $url) {
$this->collectorParser->simplifyUrl($url);
/* 初始化分頁正文內容容器 */
$paged_main_content = [];
/* 獲得內容入口頁麵內容 */
if (($result = $this->getResult($url)) !== false) {
/* 判斷采集需要采集正文內容 */
if (isset($result[self::MAIN_CONTENT_SELECTOR_ID])) {
/* 保存第一頁內容 */
$paged_main_content[] = $result[self::MAIN_CONTENT_SELECTOR_ID];
try {
$page_urls = $this->collectorParser->getContentPages($this->getHtml(), $this->getHtmlDom());
} catch (Exception $e) {
if ($this->contentPageMode === self::PAGES_INLINE) {
$page_urls = $this->getContentInlinePages($this->getHtml(), $this->getHtmlDom(), $this->contentPagesSelector);
}
}
/* 內部查找實例,用於查找分頁其他內容 */
$mainContentFinder = new Finder();
$mainContentFinder->addSelector(self::MAIN_CONTENT_SELECTOR_ID, $this->contentSelector);
while (count($page_urls) > 0) {
$page_url = array_shift($page_urls);
$page_result = $mainContentFinder->getResult($this->collectorParser->changeUrl($page_url));
if ($page_result[self::MAIN_CONTENT_SELECTOR_ID]) {
$paged_main_content[] = $result[self::MAIN_CONTENT_SELECTOR_ID];
}
if (get_class($this->collectorParser) === __NAMESPACE__ . '\\CollectorParser' && $this->contentPageMode === self::PAGES_CONTEXT) {
if ($next_url = $this->collectorParser->getContentContextPage($this->getHtml(), $this->getHtmlDom(), $this->contentPagesSelector)) {
$page_urls[] = $next_url;
}
}
}
unset($page_url, $next_url, $page_result, $result[self::MAIN_CONTENT_SELECTOR_ID]);
}
/* 處理替換工作 */
foreach ($result as $key => &$item) {
if ($selector = $this->getSelector($key)) {
$item = $this->replaceString($item, $selector);
}
}
unset($key, $item);
/* 遍曆分頁內容,並替換字符串 */
foreach ($paged_main_content as &$content) {
if ($this->contentSelector) {
$content = $this->replaceString($content, $this->contentSelector);
}
}
unset($content);
if ($paged_main_content) {
/* 創建閉包函數需要的實例 */
$collectorParser = $this->collectorParser;
$pictureMaker = $this->pictureMaker;
$downloadPicture = $this->downloadPicture;
$content_pictures = [];
/* 初始化正文圖片容器,用於返回給監聽器 */
/* 替換正文內容的圖片地址為采集後地址 */
foreach ($paged_main_content as &$content) {
$paged_content_pictures = [];
/* 初始化存儲每頁圖片容器,用於返回給監聽器 */
$content = preg_replace_callback('/<img\\s[^>]*\\ssrc="([^>]+?)"\\s[^>]*\\/?>/i', function ($match) use($collectorParser, $pictureMaker, &$content_pictures, &$paged_content_pictures, $downloadPicture) {
/* 補全圖片鏈接 */
$pic_url = $collectorParser->changeUrl($match[1]);
/* 如果需要下載圖片,則替換為目標地址 */
if ($downloadPicture) {
$pic_url = $pictureMaker->getUrl($pic_url);
}
$content_pictures[] = $pic_url;
$paged_content_pictures[] = $pic_url;
return '<img src="' . $pic_url . '" />';
}, $content);
$this->dispatch('collect_paged_main_content_success', $url, $content, $paged_content_pictures);
}
unset($collectorParser, $pictureMaker, $downloadPicture, $content, $paged_content_pictures);
/* 替換別名 */
$result[$this->contentSelectorIDAlias] = Helper::formatContent(implode('', $paged_main_content));
}
$this->dispatch('collect_content_success', $url, $result, $content_pictures);
} else {
$this->dispatch('collect_content_fail', $url);
}
}
unset($url);
if ($this->downloadPicture) {
/* 開始下載圖片 */
$this->pictureMaker->download();
}
}