Drupal数据接入及采集

作者: 游鱼思


模块对比

在Drupal中,如果你想对接外部API以引入外部数据源作为内容类型或实体(entity),有几个模块可以帮助你实现这一功能。以下是一些主要的模块和工具:

Feeds

Migrate API

手册:Migrate API | Drupal APIs | Drupal Wiki guide on Drupal.org

Migrate Plus

Migrate Source JSON

Migrate Tools

Web UI provides a front-end equivalent to many of the drush commands.

provides a user interface for site administrators to import various entities. At the moment the module only support CSV files as the source, but due to the plugin type architecture this can easily be expanded to other file formats. There are also plans in the road-map to support other common source types moving forward.

示例:Importing Data from a REST API with Entity Import and Migrate in Drupal 8 | Aten Design Group

HTTP Client Manager

根据你的具体需求和数据源的类型(如RESTful API、CSV文件、XML或JSON数据等),你可以选择最适合你的模块。一般来说,Migrate系列的模块和Feeds模块非常适合批量导入外部数据,而HTTP Client Manager和RESTful Web Services模块则更适合与外部API进行实时交互。

主要特点

使用场景

HTTP Client Manager 模块适用于需要与外部服务进行交互的 Drupal 网站。例如,如果您的网站需要从第三方 API 拉取数据、提交表单数据到外部系统或集成外部服务,HTTP Client Manager 可以简化这些任务的开发工作。

说明文档:HTTP Client Manager | Contributed module documentation | Drupal Wiki guide on Drupal.org

视频介绍:Drupal 8 Http Client Manager - YouTube

provides an interface to consume external web services. It focuses on integrating web service operations as Rules actions, but can also be used as data provider for other modules.

Feeds 模块解决的列表的批量导入,这个模块解决的是已经有了数据条目后,字段值的外部导入。

Using external data source in Drupal as Microsevice architecture is a best practice. By using this module we will be able to create a new data source as a plugin that we can find it when we configure the field type "external data source field" .

比较遗憾的是,只支持字段,而不是整个实体。

在D10中有Bug,反正我是用不了,添加字段第二步就出错了,也查不到出错原因。

将外部的数据集,转变为Drupal的 Entity

The PHPExcel module allows developers to export/import data to/from real Excel files.

Accessing RDF from other sites | Drupal.org

A SPARQL endpoint is a conformant SPARQL protocol service as defined in the

SPROT specification. A SPARQL endpoint enables users (human or other) to query

a knowledge base via the SPARQL language.

Resource Description Framework (RDF) | Drupal.org

数据处理及展示

SPARQL Entity Storage | Drupal.org   10

RDF entity | Drupal.org  10

Views Datasource | Drupal.org   7

Views Datasource is a set of plugins for Drupal Views for rendering content in a number of shareable, reusable formats based on XML, JSON and XHTML. These formats allow content in a

Migrate

Feeds

Feeds 生态

Ecosystem modules for Feeds | Drupal.org

1, Feeds模块, 这个是主模块,它是用来导入数据,开始是收割RSS数据,后来发展为导入各种数据,再后来,我们发现,基于这个模块,可以用来采集网页信息。
2, job_scheduler D10,这个是Feeds模块依赖的插件,采集时的任务调度,很多时候需要依赖于这个模块。
3, feeds_tamper D10,这个模块,用来在导入数据的时候,对数据进行预处理、也就是清理工作,非常好用的帮助模块。采集网页数据时,必备模块。
4, feeds_xpathparser D10,这个模块使得我们可以使用Xpath规则,解析数据,网页采集过来的数据,是HTML格式的,需要使用Xpath规则解析。这个也是必备模块之一。

5, feeds_crawler 模块 D7,这是一个小爬虫,方便采集各种分页列表,非常好用,网页抓取必备模块。

6, feeds_smartparser 模块 D7 ,从HTML页面中,智能提取全文,网页采集必备模块之一。
7, Feeds Self Node Processor D7 ,采集过来的节点本身,还是一个Feed种子,通过HTTP请求,抓取更详细的信息,完善自身节点,网页采集必备模块之一。 You may need this is if your data source has remote data in both a list and in more detail in its main display.

8, Views/Ctools views data export,将网页采集过来的数据,导出成各种格式,支持XML,CSV,Excel。
9, feeds_spider D7 ,采集蜘蛛,类似于feeds_crawler,网页采集模块之一。只支持D7

Feeds Fetcher POST D10 This module adds a new Fetcher type plugin to use in the Feeds module. You may use this to fetch a Download URL (i.e. JSON/XML) and specify custom parameters before getting successful responses on POST requests.

JSON Feed | Drupal.org

背景知识: Json 查询语法

总结

总的来说,D10 时代 Feeds 模块没落了。不仅安装困难,而且少了这些贡献模块后,Feeds对于爬取是半吊子支持,很难满足各种需求。

最大的痛点是,只能从列表API导入,并且还不能带分页等变量,也无法再次从详情API导入。

很少有被爬的网站会乖乖的把所有详情都塞在列表API里,还不分页吐给你,那简直是大善人。
卸载了吧,没啥用。

Feeds安装踩坑记

PHP8.3, Drupal 10.2 ,Feeds 8.x-3.0-beta4

先手工安装了一遍feeds,发现无法创建 feed type,应该是缺少库。

卸载后,删除了模块文件夹,又通过composer安装了一遍。这下库是齐了,但是整站也无法打开了。

提示: The website encountered an unexpected error. Try again later.

侦查过程:

查找资料:

The website encountered an unexpected error. [#3383226] | Drupal.org ,还需要使用 https://www.drupal.org/project/ludwig 来管理三方库。但是 ludwig 和 composer 又有冲突。

折腾过程:

1、尝试了手工安装:在web/modules/feeds,拷贝 vendor/laminas 到 web/libraries/laminas ,无效。
2、在 web/modules/contrib 、 web/modules 两个目录下,都复制一份 feeds 文件夹,依次使用:

composer remove 'drupal/feeds'
drush cr
drush pmu feeds

提示卸载成功后,再手工去以上两个目录删除残留文件。

再通过composer安装一遍,且还要将 web/modules/contrib/feeds 移动到 web/modules/feeds 下

还是提示各种错误和缺失库。

卸载:通过 composer remove drupal/ludwig 、 drush pmu feeds、 drush cedit core.extension 并移除feeds 这些方案都试过,无法完全卸载,导致Drupal后台许多页面无法访问。

还得手工创建表,解决drupal后台无法访问的问题:

CREATE TABLE `feeds_feed` (
  `fid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'The feed ID.', 
  `feed_type` varchar(128) NOT NULL COMMENT 'The feed type.',
  `bundle` varchar(128) NOT NULL COMMENT 'The feed bundle.', 
  `config` longblob COMMENT 'The feed configuration.',
  `created` int(11) NOT NULL COMMENT 'The Unix timestamp when the feed was created.',
  `updated` int(11) NOT NULL COMMENT 'The Unix timestamp when the feed was last updated.',
  PRIMARY KEY (`fid`),
  UNIQUE KEY `feed_bundle` (`bundle`),
  KEY `feed_type` (`feed_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Stores information about feeds.';

CREATE TABLE `feeds_subscription` (
  `fid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'The subscription ID.', 
  `feed_id` int(10) unsigned NOT NULL COMMENT 'The feed ID.',
  `uid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The user ID subscribed to this feed.',
  `created` int(11) NOT NULL COMMENT 'The Unix timestamp when the subscription was created.',
  PRIMARY KEY (`sid`),
  UNIQUE KEY `feed_uid` (`feed_id`,`uid`),
  KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Stores user subscriptions to feeds.';

然后在Drupal管理后台成功卸载Feeds。

3、手工安装feed到 web/modules/feeds,通过composer安装 ludwig,然后访问 /admin/reports/packages ,ludwig 自动下载了缺失的库。 ——也不行,还是提示 库 缺失。

最终解决

按照以上步骤卸载 feeds 后 。

降级到PHP 8.1(可能非必须)

手工安装feed到 web/modules/feeds

运行composer获取依赖库:composer require laminas/laminas-feed

成功。

错误排查

添加Feeds时报错

Error: Call to undefined function ini_restore() in Laminas\Feed\Reader\Reader::detectType() (line 494 of /www/wwwroot/rc_juhe_drupal/vendor/laminas/laminas-feed/src/Reader/Reader.php).
解决办法:PHP环境的 ini_restore() 函数被禁用了,开启一下即可

添加Feeds时,提示 Invalid feed URL.

解决办法:在Feed Type的设置中,关闭 "Auto detect feeds 自动检测feeds"

Feeds 使用

需要吐槽一下,Feeds的使用比较抽象和反直觉。

第一步:先在 结构 - Feed Types (/admin/structure/feeds) 中,定义好需要爬取的数据模型和映射。这一步竟然不让添加爬取地址,这是最反直觉的地方。 会用到 JsonPath 等语法。

第二步:在 内容 - Feeds ( admin/content/feed ) 中,添加具体的爬取地址。开始爬取。

Feeds在Mapping 时,会出现一些让人困惑的术语,以下是解释:

  1. Feed: 作者 - 这通常指的是Feed条目的原始作者。在RSS或Atom feed中,可能会有一个明确的作者字段。

  2. Feed: 发布于 - 指Feed条目首次公开发布的日期和时间。

  3. Feed: 已更改 - 指Feed条目最后一次被更新或修改的日期和时间。

  4. Feed: 种子ID - 一个唯一标识符,用于区分Feed中的每个条目。

  5. Feed: Feed type - Feeds模块允许你创建不同类型的Feed导入器。其实就是 admin/structure/feeds 下的条目。

  6. Feed: Importing status - 指当前Feed导入的状态,例如,是否正在导入中,导入是否完成等。

  7. Feed: Items imported - 指在上一次导入过程中成功导入的条目数量。

  8. Feed: Last import - 指最近一次Feed导入操作的日期和时间。

  9. Feed: Next import - 如果设置了自动定时导入,这将显示下一次计划导入的日期和时间。

  10. Feed: 已排入队列 - 指示有多少Feed条目在当前等待被导入。

  11. Feed: 源 - Feed的URL或源地址,即数据原始所在位置。

  12. Feed: 标题 - 指Feed条目的标题。

  13. Feed: UUID - Universal Unique Identifier(全局唯一ID)

视频教程

Feeds to import content by CSV file Drupal 10, Drupal 9, Drupal 8 | D4Drupal - YouTube

Using Feeds to integrate complex business data into Drupal: Pittsburgh 2023 - YouTube

Getting Started with #Drupal Feeds: A Beginner's Data Import Guide | TECHX | SPECBEE - YouTube