scribilicious/tinylistscraper

TinyListScraper 是一个易于使用的页面列表抓取器。其主要目标是抓取新闻和搜索结果。

1.0.2 2019-03-05 10:43 UTC

This package is auto-updated.

Last update: 2024-09-06 10:13:34 UTC


README

TinyListScraper 是一个易于使用的页面列表抓取器。其主要目标是抓取新闻和搜索结果。一些特性包括:

  • 结果缓存。
  • 易于维护和扩展抓取器。
  • 结果包含一系列字段,包括:标题、链接、图片、文本、简介文本、作者、来源、日期和标签。

要求

TinyListScraper 1.0 需要 PHP ^5.6 || ~7.0.0 || ~7.1.0 || ~7.2.0。必须加载 DOMDocumentDOMXPath 扩展。

用法

该库的最简单用法(自 7.0 版本起)如下所示

<?php
require_once __DIR__ . '/src/TinyListScraper.php';

// Load all results from all the scrapers
$oScraper = new TinyListScraper\TinyListScraper();
$aResults = $oScraper->get('search some text');
print_r($aResults);

// Load just the results from verge
$aResults = $oScraper->get('search some text', array('verge'));
print_r($aResults);

创建一个新的 Scraper

所有抓取设置文件都位于 src/scrapers/ 目录中。要创建一个新的,可以复制现有的抓取器或创建一个新的文件。格式必须是 [name].inc

一个抓取设置文件看起来像这样

<?php
$aSettings = array(
    'url' => 'https://techcrunch.com/search/{$SEARCH}',
    'source' => 'techcrunch.com',
    'list' => array(
        'container' => array(
            'selector' => '//div[contains(@class,"post-block post-block")]'
        ),
        'link' => array(
            'selector' => './/a[contains(@class,"post-block__title__link")]',
            'attribute' => 'href'
        )
    ),
    'content' => array(
        'title' => array(
            'selector' => './/meta[@name="sailthru.title"]',
            'attribute' => 'content'
        ),
        'author' => array(
            'selector' => './/meta[@name="sailthru.author"]',
            'attribute' => 'content'
        ),
        'date' => array(
            'selector' => './/meta[@name="sailthru.date"]',
            'attribute' => 'content'
        ),
        'description' => array(
            'selector' => './/meta[@property="og:description"]',
            'attribute' => 'content'
        ),
        'tags' => array(
            'selector' => './/meta[@name="sailthru.tags"]',
            'attribute' => 'content'
        ),
        'image' => array(
            'selector' => './/meta[@name="sailthru.image.full"]',
            'attribute' => 'content'
        ),
        'thumb' => array(
            'selector' => './/meta[@name="sailthru.image.thumb"]',
            'attribute' => 'content'
        ),
        'text' => array(
            'selector' => './/div[contains(@class,"article-content")]/p'
        )
    )
);

一些重要提示:{$SEARCH} 应该替换为搜索字符串出现的 URL 位置。使用 DOMXPath 格式来查找元素。更多信息请参阅 https://php.ac.cn/manual/de/class.domxpath.php

当前抓取器

  • Mashable
  • Lifehacker
  • Verge
  • Gizmodo
  • Techcrunch