将备份书签(html 文件)转换为包含每个书签的“URL 文件”的文件夹

将备份书签(html 文件)转换为包含每个书签的“URL 文件”的文件夹

举一个简单的例子:

我有一个用于 Google Chrome 备份书签的 html 文件:bookmarks.html

bookmark.html 是没有文件夹、只有书签的备份。

我想将 bookmarks.html 转换为包含每个书签的“URL 文件”的文件夹 BOOKMARKS。

每个书签名称都会转换为文件名。每个文件都可以是内容中的 HTML 文件,如下所示:

<html>
<head>
<meta http-equiv="refresh" content="0; url=http://example.com/" />
</head>
</html>

https://superuser.com/questions/986527/how-to-create-a-hyperlink-file

这是bookmarks.html的内容。

<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file.
     It will be read and overwritten.
     DO NOT EDIT! -->
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
    <DT><H3 ADD_DATE="1566283592" LAST_MODIFIED="1566283659" PERSONAL_TOOLBAR_FOLDER="true">Bookmarks bar</H3>
    <DL><p>
        <DT><A HREF="https://www.google.com/" ADD_DATE="1566283606" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACIklEQVQ4jYWSS0iUURTHf/fe8RvHooE2VlT2FNqUGWmNEYUR9lhEEVJhUIsoXOQuap1Rq6KHNQt3LaPAIOxhlNTChUwLMU3NR1CklUzg6xvPd1ro2KhTHjjcA/e8/uf/hzmmqsUiEheRLhHxp/2TiDxQ1aK5+ZmFeSJSrwuYiMRVNZKuMxnFz51zu9T3GX/6iPGmRqS/F5WAUMEawuUVRI5UYjwPEWl2zlUYY8YMgIjUW2vPBkPfSV6uYbKvJ+uW3rZSojfuABAEQdw5d96oajHQqr7P8IUqpL8X43lEjp3EK4mBtfgt75l4+4po7U3cytWZPbcyjUlTidv642ipDu7foX7bh2zgs92jDhHpUlWdbNmuEw15OvqweqE7ZjboCAEFADrSjs1LkRM7NAt3+bWRebfYudFx9XguwFqbwePs9z/mT/6NLdAHMBpex28W0/C1Y1Zy05VFM75nUwiAZVGT/v5sgdcA3UurOPUrxvXOFhJD7fOmdn4LeNc5NbpkfWimv5mWZ8KXFKdfXqInOYBnc6gsPEjZ8mKssbQOtvEkMczYl0oK8z3un4lgppbYkhZS3Fp7bnD0Jxeba+lODmTFviFcxq29NeRHDUEQ1DnnqtNSjohIo3Nutx+keNz9gmf9zfQkB0ChYMkK9q2KcaLwMJFQGFV9Y4w5YIwZzyBBI2lRLcD9PVXN/SdFqlokInUi0iEiE9P+UUTuqurmufl/AKTzsFGmvUNUAAAAAElFTkSuQmCC">Google</A>
        <DT><A HREF="https://stackoverflow.com/" ADD_DATE="1566283626" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABXklEQVQ4jbWQsUsCYRjGn/fuSu/Sk3ALmlzNtoagKRqSaHMKGkKhEOV0KWispSXPQaglAnNobOgfaCyIcgicmxO9zFPv/N5WwTs5gt7x+5739/2eDwgw/bK67HcnBQG4Ag3L0LJ/BoBFDuDzTiGUCAywDC3bNbRtANCrwxaBziRZanAGcjADwR8AX1uGesEZyFGzXwO43VsKn07GaJa5lY/GMefUAYooEvaELDnCEW9M2I1V7GdPg04hlLAM7dYqqut67ftLNwdpMB5dgRfXdVMgHIFpx9egfbwYk0eDA2LKAWJMkK6cUOhOGdkpZmoQiy29OmwFq1AKb5CgQyakAXqQJKpELn/eJzPK1JKhPhHjk4EmMzUVmU/coVLkeXff672pk155YXUsxikCJQFeYVCSgCiAV920N311b+r37FslH413S+qaV86rggfIBbG38RRAN+2ZHzsTMKvGv80vvziHGAusG84AAAAASUVORK5CYII=">Stack Overflow</A>
        <DT><A HREF="https://www.youtube.com/" ADD_DATE="1566283636" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABCElEQVQ4ja2TMU4DMRBF38wiEFIqKFYIUSAtJ8kNcoood8jJopwgRyANNCg1aSJl/Sls73rdoBC+NLI982c8/raNAgIHLC2NKZRHg1BHavKvyDlWOGbAG9ACT8RuSgTgCzgA7wbHstpC8Ck4CSSQzDTMp3ZK3EVOvhN8pGAvCILzYGbnYh0SRynn1oEX4JGpME7bOmaO5Lh7daQAPADPDtwnLSKhaaI2q5Wx2RhdZ4RgTIXOBWde7WyYRWLfw3wOux2s12PhaaFQK30xbqqzCSnu1DSw3cJyCfv9GI+Wu3AEneCY1M23ENS2QWZx7h4G/8j7FrxefY25rz8/pH97ytd/puS8+Dv/AFRGv3m23DwSAAAAAElFTkSuQmCC">YouTube</A>
        <DT><A HREF="https://www.facebook.com/" ADD_DATE="1566283659" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAArUlEQVQ4jWN0yti8leH/fy8GcgAj4zYmsjUzMDAw/P/vxUS2ZihgIUYRDycrQ1aYNoOlnjgDLxcbw9yNNxiW77hNvAGZodoM7paycD4bC8LhRBlgrivOwMDAwNC96ALDzuNPGP7//0+aAYK8bAwMDAwMO449xpCjOBAZndI3/ccluXeGL1bxvqWXGLYefki+Cx6/+EKcC9Bd4pyxGUOO4jAYFgYwMm4jWzcj4zYA2mQtm7CTmFoAAAAASUVORK5CYII=">Facebook </A>
    </DL><p>
</DL><p>

答案1

你可以采用以下方法:

  1. 将 bookmarks.html 解析为
    • 保留包含 HREF 的行
    • 仅保留这些行中 HREF="..." 的内容
  2. 使用这些字符串
    • 删除协议和斜线 ( /)
    • 用好名字填充模板文件

  • 在您喜欢的编辑器中输入以下代码:


#! /bin/bash

sites=$(sed '/HREF/!d' < $1 | sed 's/.*HREF="\([^ "]*\).*/\1/')

for site in $sites
do
    file=$(echo $site | sed 's/https*:\/\///' | sed 's/\/.*//')

    cat << EOF > $file
    <html>
    <head>
    <meta http-equiv="refresh" content="0; url=$site" />
    </head>
    </html>
    EOF

done
  • 另存为extract.sh
  • 在终端中,使其可执行chmod +x extract.sh
  • 用它:./extract.sh /path/to/bookmark.html

相关内容