跳转到主要内容

爬取和解析静态网站并导入到Plone

项目描述

FunnelWeb - 内容转换变得简单

轻松将现有HTML内容转换为Plone。

代码仓库:

http://github.com/collective/funnelweb

有关问题和评论请:

http://github.com/collective/funnelweb/issues

在以下位置报告错误:

http://github.com/collective/funnelweb/issues https://github.com/collective/transmogrify.webcrawler/issues https://github.com/collective/transmogrify.htmlcontentextractor/issues https://github.com/collective/transmogrify.siteanalyser/issues https://github.com/collective/transmogrify.ploneremote/issues

简介

FunnelWeb是一个网络爬虫,可以从现有网站中提取网站内容,如标题、描述、图片和内容块。它过滤这些内容并将它们上传到一个新的网站,该网站使用Plone CMS。它提供了许多调整内容迁移方式的选项。当你想要迁移一个不使用CMS或没有工具可以直接从网站数据库迁移内容的网站时,这是一个非常有价值的工具。

对于那些熟悉collective.transmogrifier的人来说,FunnelWeb是一个预构建的管道,它结合了来自四个不同包的蓝图(transmogrify.webcrawlertransmogrify.htmlcontentextractortransmogrify.siteanalysertransmogrify.ploneremote)。由于collective.transmogrifier框架的灵活性质,高级用户可以向他们的转换过程添加更多步骤。

FunnelWeb脚本执行的工作可以分为四个部分

  1. 爬取网站,包括本地缓存以便后续爬取更快,以及过滤掉不需要的内容(transmogrify.webcrawler

  2. 移除样板/模板(自动或通过规则),以便仅保留内容(transmogrify.htmlcontentextractor

  3. 分析网站结构以改进内容质量,包括确定标题、默认视图、要创建的对象类型、导航中要显示的内容等(transmogrify.siteanalyser

  4. 上传到CMS,如Plone,或将清理后的HTML保存到本地目录(transmogrify.ploneremote

FunnelWeb使用mr.migrator框架,它允许运行FunnelWeb的collective.transmogrifier管道

  1. 在Plone内部。有关安装方法,请参阅mr.migrator

  2. 一个可以通过zc.buildout安装的命令行脚本。内容通过其网络服务API上传到Plone

命令行安装

您可以通过easy_install安装

$> easy_install funnelweb

这可以通过以下方式运行

$> buildout

或者,FunnelWeb可以通过buildout配方安装(参见zc.buildout

[buildout]
parts += funnelweb

[funnelweb]
recipe = funnelweb

$> buildout init
$> bin/buildout

这可以通过以下方式运行

$> bin/funnelweb

这里提供的示例将假设通过buildout安装

命令行配置

FunnelWeb组织成一系列步骤,通过这些步骤爬取的项目最终被上传。每个步骤都有一个或多个配置选项,因此您可以自定义导入过程以满足您的需求。几乎所有的导入都需要一定程度的配置。

FunnelWeb提供了三种配置管道的方法。

使用本地管道配置

您可以创建自己的pipeline.cfg文件,它覆盖并扩展了默认的FunnelWeb管道。

例如,创建一个名为pipeline.cfg的文件,内容如下

[transmogrifier]
include = funnelweb.remote

[crawler]
url=http://collective-docs.readthedocs.org/en/latest/

这将覆盖爬虫蓝图设置“url”。您可以通过以下方式运行它

$> bin/funnelweb --pipeline=pipeline.cfg

您可以通过以下命令查看FunnelWeb.remote管道及其所有选项

$> bin/funnelweb --show-pipeline

您还可以保存此管道并根据您的需求进行自定义

$> bin/funnelweb --show-pipeline > pipeline.cfg
$> {edit} pipeline.cfg
$> bin/funnelweb --pipeline=pipeline.cfg

命令行参数

可以通过命令行覆盖管道中的任何参数

例如

$> bin/funnelweb --crawler:url=http://www.whitehouse.gov

所有参数都是 –(步长:参数)=值。每个配置键的第一个部分是步长,例如 crawler。第二部分是特定步长的配置选项。例如 url。然后跟着 = 和值或值列表。

某些选项需要在 buildout 部分中多行。这些可以通过命令行重复相同的参数来覆盖,例如。

$> bin/funnelweb --crawler:ignore=\.mp3 --crawler:ignore=\.pdf

您可以通过以下方式查看所有参数的列表

$> bin/funnelweb --help

Buildout覆盖

任何命令行覆盖也可以“烘焙”到 funnelweb 脚本中。例如。

[buildout]
parts += funnelweb

[funnelweb]
recipe = funnelweb
crawler-url=http://www.whitehouse.gov
pipeline=pipeline.cfg

任何形式为

[step]
blah = blah

的参数将在 buildout 中变成

[funnelweb]
recipe = funnelweb
step-blah = blah

以及在命令行中

bin/funnelweb --step:blah=blah

配置选项

可以配置的完整步骤列表以及每个步骤的 transmogrifier 蓝图

  1. 爬取

crawler:

transmogrify.webcrawler

cache:

transmogrify.webcrawler.cache

typeguess:

transmogrify.webcrawler.typerecognitor

drop:

collective.transmogrifier.sections.condition

  1. 模板

template1:

transmogrify.htmlcontentextractor

template2:

transmogrify.htmlcontentextractor

template3:

transmogrify.htmlcontentextractor

template4:

transmogrify.htmlcontentextractor

templateauto:

transmogrify.htmlcontentextractor.auto

  1. 网站分析

sitemapper:

transmogrify.siteanalyser.sitemapper

indexguess:

transmogrify.siteanalyser.defaultpage

titleguess:

transmogrify.siteanalyser.title

attachmentguess:

transmogrify.siteanalyser.attach

hideguess:

transmogrify.siteanalyser.hidefromnav

urltidy:

transmogrify.siteanalyser.urltidy

addfolders:

transmogrify.pathsorter

changetype:

collective.transmogrifier.sections.inserter

  1. 上传

ploneupload:

transmogrify.ploneremote.remoteconstructor

ploneupdate:

transmogrify.ploneremote.remoteschemaupdater

plonehide:

transmogrify.ploneremote.remotenavigationexcluder

publish:

collective.transmogrifier.sections.inserter

plonepublish:

transmogrify.ploneremote.remoteworkflowupdater

plonealias:

transmogrify.ploneremote.remoteredirector

ploneprune:

transmogrify.ploneremote.remoteprune

localupload:

transmogrify.webcrawler.cache

或者您可以使用命令行帮助来查看可用选项的列表

$> bin/funnelweb --help

以下详细介绍了这些步骤最常用的配置选项。

爬取 - HTML导入

Funnelweb 可以从实时网站、磁盘上的文件夹或从实时网站检索的 HTML 文件(可能仍然包含指向该网站的绝对链接)导入 HTML。

Funnelweb只能导入它能够爬取的内容,即从HTML链接的内容。如果您的网站包含JavaScript链接或受密码保护的内容,您可能需要执行一些额外步骤才能让Funnelweb爬取您的内容。

要爬取一个实时网站,向爬虫提供起始爬取的HTTP基本URL。此URL必须是您想要从网站获取的所有其他URL的开头。

例如

$> bin/funnelweb --crawler:url=http://www.whitehouse.gov --crawler:max=50  --ploneupload=http://admin:admin@localhost:8080/Plone

将限制爬虫仅爬取前50页,然后将内容转换为本地Plone网站。

您爬取的网站将在本地缓存,因此如果您再次运行Funnelweb,它将运行得更快。如果您想禁用本地缓存,请使用

$> bin/funnelweb --cache:output=

如果您想重置缓存,刷新其数据,将爬虫的缓存设置为空

$> bin/funnelweb --crawler:cache=

默认情况下,缓存存储在var/funnelwebcache/{site url}/。您可以使用以下命令将其设置为另一个目录

$> bin/funnelweb --cache:output=my_new_dir

您还可以通过使用file://风格的URL仅爬取包含相对链接的本地HTML目录

$> bin/funnelweb --crawler:url=file:///mydirectory

或者如果本地目录包含从网站保存的HTML,可能包含绝对URL,则可以将此设置为缓存。爬虫将始终首先查找缓存

$> bin/funnelweb --crawler:url=http://therealsite.com --crawler:cache=mydirectory

以下不会爬取大于4Mb的内容

$> bin/funnelweb --crawler:maxsize=400000

要跳过通过正则表达式爬取的链接

[funnelweb]
recipe = funnelweb
crawler-url=http://www.whitehouse.gov
crawler-ignore = \.mp3
                 \.mp4

如果Funnelweb在解析某些页面的HTML时遇到困难,您可以在解析之前预处理HTML。例如

[funnelweb]
recipe = funnelweb
crawler-patterns = (<script>)[^<]*(</script>)
crawler-subs = \1\2

如果您想跳过处理具有特定MIME类型的链接,可以使用drop:condition选项。此TALES表达式确定将进一步处理的内容

[funnelweb]
recipe = funnelweb
drop-condition: python:item.get('_mimetype') not in ['application/x-javascript','text/css','text/plain','application/x-java-byte-code'] and item.get('_path','').split('.')[-1] not in ['class']

模板

Funnelweb有一个内置的聚类算法,它尝试自动从HTML模板中提取内容。这很慢,并且并不总是有效的。通常您需要输入自己的模板提取规则。

如果您想关闭自动模板

$> bin/funnelweb --templateauto:condition=python:False

规则的形式为

(title|description|text|anything) = (text|html|optional) XPath

例如

[funnelweb]
recipe = funnelweb
crawler-site_url=http://www.whitehouse.gov
ploneupload-target=http://admin:admin@localhost:8080/Plone
template1-title       = text //div[@class='body']//h1[1]
template1-_delete1    = optional //div[@class='body']//a[@class='headerlink']
template1-_delete2    = optional //div[contains(@class,'admonition-description')]
template1-description = text //div[contains(@class,'admonition-description')]//p[@class='last']
template1-text        = html //div[@class='body']

请注意,对于单个模板(例如,template1),所有XPath都需要匹配,否则该模板将被跳过,然后尝试下一个模板。如果您想使单个XPath对于模板匹配不是必需的,请在XPath之前使用关键词optionaloptionaltext代替texthtml

在默认管道中,有四个模板称为template1template2template3template4

当XPath应用于单个模板时,它匹配的HTML将从页面中删除。同一模板中的另一个规则不能匹配相同的HTML片段。

如果内容部分对Plone(例如,冗余文本、标题或描述)没有用处,它是一种有效地从内容中删除该HTML的方法。

为了帮助调试模板规则,您可以设置调试模式

$> bin/funnelweb --template1:debug --template2:debug

在模板auto上设置调试模式将为您提供有关它使用的规则的详细信息。

$> bin/funnelweb --templateauto:debug
...
DEBUG:templateauto:'icft.html' discovered rules by clustering on 'http://...'
Rules:
      text= html //div[@id = "dal_content"]//div[@class = "content"]//p
      title= text //div[@id = "dal_content"]//div[@class = "content"]//h3
Text:
      TITLE: ...
      MAIN-10: ...
      MAIN-10: ...
      MAIN-10: ...

有关XPath的更多信息,请参阅

网站分析

为了提供一个看起来更干净的Plone网站,有几个选项可以分析整个爬取网站并进行清理。这些默认是关闭的。

要确定一个项是否为容器的默认页面(它包含许多指向该容器中项的链接,即使它们不在该文件夹中),然后将其移动到该文件夹,请使用

$> bin/funnelweb --indexguess:condition=python:True

您可以通过分析反向链接文本自动找到更好的页面标题

[funnelweb]
recipe = funnelweb
titleguess-condition = python:True
titleguess-ignore =
      click
      read more
      close
      Close
      http:
      https:
      file:
      img

以下将找到仅被一个页面引用的项,并将它们移动到以该页面作为默认视图的新文件夹中

$> bin/funnelweb --attachmentguess:condition=python:True

或者以下内容仅移动图像附件,并使用index-html作为新创建文件夹默认页面的新名称

[funnelweb]
recipe = funnelweb
attachmentguess-condition = python: subitem.get('_type') in ['Image']
attachmentguess-defaultpage = index-html

以下将根据TALES表达式整理URL

$> bin/funnelweb --urltidy:link_expr="python:item['_path'].endswith('.html') and item['_path'][:-5] or item['_path']"

如果您想在上传前移动内容,也可以使用urltidy步骤,例如

$> bin/funnelweb --urltidy:link_expr=python:item['_path'].startswith('/news') and '/otn/news'+item['path'][5:] or item['_path']

如果您想隐藏导航中的内容,可以使用hideguess

$> bin/funnelweb –hideguess:condition=python:item[‘path’]==’musthide’

上传到Plone

上传通过远程XML-RPC调用进行,因此可以用于任何正在运行的网站。

要设置网站上传的位置,请使用

$> bin/funnelweb --ploneupload:target=http://username:password@myhost.com/myfolder

目前只支持通过在url中设置用户名和密码的基本认证。如果没有设置目标,则网站将被抓取但不会上传。

如果您想更改上传的内容类型

$> bin/funnelweb --changetype:value=python:{'Folder':'HelpCenterReferenceManualSection','Document':HelpCenterLeafPage}.get(item['_type'],item['_type'])

这将设置项目类型的新值。您可以将此设置为条件,例如

$> bin/funnelweb --changetype:condition=python:item['_path].startswith('/news')

或者使用更复杂的表达式来设置新类型

$> bin/funnelweb –changetype:value=python:item[‘_path’].startswith(‘/news’) and ‘NewNewsType’ or item[‘_type’]

默认情况下,funnelweb会根据原始抓取的URL自动创建Plone别名,以便任何旧链接都会自动重定向到新清理后的URL。您可以通过以下方式禁用此功能

$> bin/funnelweb --plonealias:target=

您可以通过以下方式设置哪些项目将发布到哪个状态

[funnelweb]
recipe = funnelweb
publish-value = python:["publish"]
publish-condition = python:item.get('_type') != 'Image' and not options.get('disabled')

当该项目仅从内容区域内链接时,Funnelweb会隐藏某些项目从Plone的导航中。您可以通过以下方式禁用此行为

$> bin/funnelweb --plonehide:target=

您可以使用以下方式获取将要上传的本地文件表示

$> bin/funnelweb --localupload:output=var/mylocaldir

示例

请随意分叉并添加您自己的示例,用于从常见网站或CMS中提取内容

Read The Docs

例如,以下buildout将创建一个funnelweb脚本,该脚本将普通sphinx文档转换为PloneHelpCenter内的远程Plone内容

[transmogrifier]
include = funnelweb.remote

[crawler]
url=http://collective-docs.readthedocs.org/en/latest/
ignore=
        cgi-bin
        javascript:
        _static
        _sources
        genindex\.html
        search\.html
        saesrchindex\.js

[template1]
title = text //div[@class='body']//h1[1]
description = optional //div[contains(@class,'admonition-description')]/p[@class='last']/text()
text = html //div[@class='body']
# Fields with '_' won't be uploaded to Plone so will be effectively removed
_permalink = text //div[@class='body']//a[@class='headerlink']
_label = optional //p[contains(@class,'admonition-title')]
_remove_useless_links = optional //div[@id = 'indices-and-tables']

# Images will get titles from backlink text
[titleguess]
condition = python:True

# Pages linked to content will be moved together
[indexguess]
condition = python:True

# Hide the images folder from navigation
[hideguess]
condition = python:item.get("_path","").startswith('_images') and item.get('_type')=='Folder'

# Upload as PHC instead of Folders and Pages
[changetype]
value=python:{'Folder':'HelpCenterReferenceManualSection','Document':'HelpCenterLeafPage'}.get(item['_type'],item['_type'])

# Save locally for debugging purposes
[localupload]
output=manual

# All folderish content should be checked if they contain
# any items on the remote site which are not presented locally. including base folder
[ploneprune]
condition=python:item.get('_type') in ['HelpCenterReferenceManualSection','HelpCenterReferenceManual'] or item['_path'] == ''

Joomla

#TODO

Wordpress

#TODO

Drupal

#TODO

其他

在此处添加您自己的示例

控制日志

您可以通过设置调试命令行开关来在特定集合上显示额外的调试输出。例如,要查看模板匹配失败的其他详细信息

$> bin/funnelweb --template1:debug

直接使用transmogrifier(高级)

您可能需要为您的特定转换用例插入额外的转换步骤。为此,您可以通过扩展funnelweb的底层transmogrifier管道来完成此操作。Funnelweb使用transmogrifier管道执行所需的转换,所有命令行和配方选项都引用管道中的选项。

您可以通过以下命令查看管道及其所有选项

$> bin/funnelweb --show-pipeline

您还可以保存此管道并根据您的需求进行自定义

$> bin/funnelweb --show-pipeline > pipeline.cfg
$> {edit} pipeline.cfg
$> bin/funnelweb --pipeline=pipeline.cfg

自定义管道允许您添加标准funnelweb工具未预先考虑的自定义转换。

查看transmogrifier文档以了解如何添加自己的蓝图或将现有的蓝图添加到您的自定义管道中。

使用外部蓝图

如果您决定需要自定义管道,并希望安装使用蓝图而不是funnelweb或transmogrifier中已包含的蓝图进行转换的转换步骤,您可以使用funnelweb buildout部分中的eggs选项来包含它们

[funnelweb]
recipe = funnelweb
eggs = myblueprintpackage
pipeline = mypipeline.cfg

但是,这仅在蓝图包包含其setup.py中的以下setuptools入口点时才有效

entry_points="""
      [z3c.autoinclude.plugin]
      target = transmogrify
      """,
      )

funnelweb管道中可用的属性

在 funnelweb 中使用默认蓝图时,以下是一些将附加到每个蓝图可访问项上的属性。这些属性可以在各种条件语句等以及您自己的蓝图中使用。

_site_url

传递给网络爬虫的 URL 的基础部分。

_path

URL 的其余部分。 _site_url + _path = URL

_mimetype

爬虫返回的 MIME 类型。

_content

爬取项的内容,包括图像、文件或 HTML 数据。

_orig_path

被爬取项的原始路径。这有助于设置重定向,以便在内容迁移后不会出现 404 错误。

_sort_order

表示该项被爬取顺序的整数。有助于确定在服务器上创建的文件夹中项目应按何种顺序排序,如果您的网站有从上到下排序的链接导航。

_type

由“typeguess”步骤返回的待创建对象的类型。

titledescriptiontext等。

模板步骤通常将从 _content 中提取的内容创建为字段。

_template

模板步骤将未分离到不同字段的 HTML 留在这个属性中。

_defaultpage

在文件夹项上设置,您想告诉上传步骤将 _defaultpage 中提到的包含项设置为该文件夹上的默认页面,而不是内容列表。

_transitions

指定在项上传或更新后要对其执行的工作流操作。

_origin

这用于与 transmogrify.siteanalysis.relinker 蓝图内部结合使用,作为一种方式,告知它已更改 _path,现在希望重链接器找到任何引用 _origin 的链接,现在应指向 _path

Funnelweb管道

查看 http://github.com/collective/funnelweb/blob/master/funnelweb/remote.cfg 或输入

$> bin/funnelweb --show-pipeline

贡献

funnelweb 的代码本身相当简洁。它只是设置并运行一个 transmogrifier 管道。实际的工作实际上是由五个包完成的,每个包都包含一个或多个 transmogrifier 蓝图。这些是

网络爬虫

http://pypi.python.org/pypi/transmogrify.webcrawler https://github.com/djay/transmogrify.webcrawler

HTML 内容提取器

http://pypi.python.org/pypi/transmogrify.htmlcontentextractor https://github.com/djay/transmogrify.htmlcontentextractor

网站分析器

http://pypi.python.org/pypi/transmogrify.siteanalyser https://github.com/djay/transmogrify.siteanalyser

路径排序器

http://pypi.python.org/pypi/transmogrify.pathsorter https://github.com/djay/transmogrify.pathsorter

PloneRemote

http://pypi.python.org/pypi/transmogrify.ploneremote https://github.com/djay/transmogrify.ploneremote

每个都有自己的问题跟踪器,我们将接受新功能或错误修复的拉取请求。文档和测试的当前状态尚未达到高水平。

历史

  • 2008年构建,用于导入大型企业内网

  • 2009年发布了 pretaweb.funnelweb(已弃用)。集成到 Plone UI > 操作 > 导入

  • 2010年将蓝图拆分为 pypi 上的 transmogrify.* 版本

  • 2010年 collective.developermanual sphinx 使用 funnelweb 蓝图

  • 2010年发布 funnelweb Recipe + Script

  • 2011年将 runner 拆分到 mr.migrator

贡献者

作者“Dylan Jay”,贡献者“Vitaliy Podoba”,贡献者“Rok Garbas”,贡献者“Mikko Ohtamaa”,贡献者“Tim Knapp”,贡献者

变更历史

1.1.1 (2012-06-28)

  • 修复 setup.py 中的问题,该问题破坏了 buildout

1.1 (2012-04-28)

  • 设置默认管道,以便可以在不使用 buildout 的情况下使用 [djay]

  • 更好的文档 [djay]

  • 更改推荐使用 funnelweb 的方式 [djay]

  • 新 sitemapper 步骤 [djay]

  • 现在可以爬取 GET 请求 [djay]

  • 在 transmogrify.* 依赖中进行了各种修复。请参阅变更日志 [djay]

1.0 (2011-06-29)

  • 修复默认命令行管道到 funnelweb.remote

  • 删除 runner 代码并签入 ttw.cfg

  • funnelweb 现在依赖于 mr.migrator

  • 改进 urltidy 以处理 .asp .php

  • 将 index.asp 和 index.php 包含为默认页面

  • 处理 buildout 中的覆盖管道

  • 添加 –show-pipeline 命令

  • 修复 –pipeline 的处理

1.0b7 (2011-02-12)

  • 修复命令行覆盖中的错误

  • 仅在需要时打开缓存文件,以免用完句柄

  • 遵循 http-equiv 刷新链接

  • 不剥离 HTML 头部

1.0b6 (2011-02-06)

  • 默认关闭 templateauto

  • 添加 hideguess 步骤。目前只是手动设置要隐藏的内容

  • 从命令行允许多行值覆盖

  • 文件使用文件指针以减少内存使用

  • 缓存将 .metadata 文件保存以记录和回放头信息

  • ploneremote:修复调试输出中的错误

  • 如果文本为 None,则显示错误

  • 修复重写链接中的错误字符问题

  • 修复丢失项目中的错误

  • 模板:处理 xpaths 中的 ‘/text()’

  • 模板:新的‘optionaltext’规则格式

  • 在导入根上设置默认页面

1.0b5 (2010-12-13)

  • 修复命令行帮助的排序

  • 修复 @debug 的帮助

1.0b4 (2010-12-13)

  • 修复由缓存引起的编码问题

  • 更好的调试

  • 命令行用于打开调试信息

  • 脚本安装使用 buildout 部分名称

  • 额外的文档

  • 命令行帮助

1.0b3 (2010-11-20)

  • 修复 –pipeline 选项

  • 修复 eggs= 选项

  • 添加修剪支持

  • 删除 transmogrify.htmltesting 作为依赖项

  • 改进文档 [Jean Jordaan]

  • 将主要仓库移至 github collective https://github.com/collective/funnelweb

1.0b2 (2010-11-09)

  • 由于它创建与较旧 zope 安装的版本冲突,已删除 z3c.recipe.scripts 作为依赖项。[“Dylan Jay”]

  • 使默认缓存放置在特定域的目录中 [“Dylan Jay”]

  • 对网站分析器设置条件,并默认关闭

1.0b1 (2010-11-08)

  • 首次发布,将新的和原始的 funnelweb 蓝图集成到文档化的命令行/buildout 界面中 [“Dylan Jay”]

下载

项目详细信息


下载文件

下载适用于您平台的应用文件。如果您不确定该选择哪个,请了解有关安装包的更多信息。

源分布

funnelweb-1.1.1.zip (61.1 kB 查看哈希值)

上传时间:

支持