从网页获取文本和链接

从网页获取文本和链接

我想要一个脚本,使用 curl 下载网页,将其通过管道传输到 w3m,然后删除除文本和链接以外的所有内容。

是否可以为 w3m 的 -T 选项指定多种内容类型,如何指定?

为了进一步阐明我的问题,这里有一个例子:

curl --user-agent "Mozilla/4.0" https://askubuntu.com/questions -s | w3m -dump -T text/html

它只返回 Ask Ubuntu 问题页面的文本,但没有链接。如果 w3m 无法做到这一点,还有其他工具能够同时抓取文本和链接吗?

答案1

嗯,经过我自己的广泛研究,我猜测,没有这样的工具......

然而,无论如何,我确实发现 hxnormalize 使得编写我需要的特定脚本变得相对简单。

答案2

您可以使用lynx -dump。它将[16]在每个链接前包含一个数字,然后在文档末尾包含一个 URL 列表。

对于管道用法,您可以使用lynx -dump -force_html -stdin。但是,它无法正确处理相对链接,因为它不知道原始 URL。

因此,最好的办法就是lynx -dump http://.../不要分开curl

答案3

我认为-o display_link_number=1可以实现您的要求,例如:

$ w3m -dump -o display_link_number=1 http://example.org
Example Domain

This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.

[1]More information...


References:

[1] https://www.iana.org/domains/example

相关内容