python - 单个 Scrapy 项目与多个项目

标签 python web-scraping scrapy screen-scraping scrape

我对如何存储所有蜘蛛感到困惑。这些蜘蛛将通过使用命令行调用和从 stdin 读取的项目输入 Apache NiFi 来使用。我还计划让这些蜘蛛的子集在单独的网络服务器上使用 scrapyrt 返回单个项目结果。我需要在具有不同项目模型的许多不同项目中创建蜘蛛。它们都将具有相似的设置(例如使用相同的代理)。

我的问题是构建我的 scrapy 项目的最佳方式是什么?

  1. 将所有蜘蛛放在同一个存储库中。提供一种简单的方法来为项目加载器和项目管道创建基类。
  2. 将我正在处理的每个项目的蜘蛛分组到单独的存储库中。这样做的优点是允许项目成为每个项目的焦点,并且不会变得太大。无法共享通用代码、设置、蜘蛛监视器 (spidermon) 和基类。尽管存在一些重复,但这感觉是最干净的。
  3. 仅打包我计划在 NiFi 存储库中使用非实时的爬虫以及另一个存储库中的实时爬虫。优点是我可以将爬虫保留在实际使用它们的项目中,但仍然可以使用它们集中/盘旋哪些蜘蛛用于哪些项目。

感觉正确答案是#2。与特定程序相关的蜘蛛应该在自己的 scrapy 项目中,就像您为项目 A 创建 Web 服务一样,您不会说哦,我可以将项目 B 的所有服务端点都扔到同一个服务中,因为那是我的所有服务都将驻留在其中,即使某些设置可能会重复。可以说,一些共享代码/类可以通过单独的包共享。

你觉得怎么样?你们都是如何构建 scrapy 项目以最大限度地提高可重用性的?同一项目与单独项目的界限在哪里?它基于您的 Item 模型或数据源吗?

最佳答案

推荐来自 Google 群组帖子“Single Scrapy Project vs. Multiple Projects for Various Sources”的 Jakob:

whether spiders should go into the same project is mainly determined by the type of data they scrape, and not by where the data comes from.

Say you are scraping user profiles from all your target sites, then you may have an item pipeline that cleans and validates user avatars, and one that exports them into your "avatars" database. It makes sense to put all spiders into the same project. After all, they all use the same pipelines because the data always has the same shape no matter where it was scraped from. On the other hand, if you are scraping questions from Stack Overflow, user profiles from Wikipedia, and issues from Github, and you validate/process/export all of these data types differently, it would make more sense to put the spiders into separate projects.

In other words, if your spiders have common dependencies (e.g. they share item definitions/pipelines/middlewares), they probably belong into the same project; if each of them has their own specific dependencies, they probably belong into separate projects.

Pablo Hoffman 是 Scrapy 的开发者之一,他在另一个帖子“Scrapy spider vs project”中回复:

...recommend to keep all spiders into the same project to improve code reusability (common code, helper functions, etc).

We've used prefixes on spider names at times, like film_spider1, film_spider2 actor_spider1, actor_spider2, etc. And sometimes we also write spiders that scrape multiple item types, as it makes more sense when there is a big overlap on the pages crawled.

关于python - 单个 Scrapy 项目与多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57861326/

相关文章:

python - 接受 Django 形式的日期时间 isoformat

python - netloc 是什么意思?

python - 检查字符串是否在字符串中

java - 如何在 Java 中筛选 Ajax 站点?

python - Scrapy 中的持久重复过滤

Python:生成可用于 MySQL 的日期时间字符串

python - 使用 scrapy 将抓取的项目保存到 json/csv/xml 文件

python - Apache 时间问题

python - 使用 Python 抓取 Twitter 页面

python - 如何使用 Beautiful Soup 只选择多个表中的一个