从 html 页面获取链接

Raf*_*ael 2 linux html terminal python shell-script

我有一个 txt 文件,其中有几个 html 文件链接。我需要访问此 txt 中的每个链接并获取其中的链接并将其保存到另一个 txt 文件中。

如何为 Linux 终端/shell 脚本/python 执行此操作?

slh*_*hck 5

安装lynx,然后:

lynx -listonly -nonumbers -dump input.html > links.txt
Run Code Online (Sandbox Code Playgroud)

确保您的输入文件具有.html扩展名。

例如:

$ cat test.html
<a href="http://superuser.com">test</a>
http://google.com
$ lynx -listonly -nonumbers -dump test.html
http://superuser.com/
Run Code Online (Sandbox Code Playgroud)

如果您有一个指向需要从中获取链接的 HTML 文件的文本文件,则可以对其进行迭代:

while read -r file; do
  lynx -listonly -nonumbers -dump "$file" > "${file%.*}.txt
done < input.txt
Run Code Online (Sandbox Code Playgroud)

这将读取文本文件中的每一行,使用 lynx 提取链接,并将它们写入与它们指向的 HTML 文件具有相同基本名称的 .txt 文件中。