如何使用 wget --recursive 建立包含 index.html 的目錄?

如何使用 wget --recursive 建立包含 index.html 的目錄?

我很高興wget -r這些東西的工作原理和下載方式。

我已經設定了一個本地主機伺服器,該伺服器為網站提供服務,頁面如下所示:

http://localhost:8080/
http://localhost:8080/foo
http://localhost:8080/bar
http://localhost:8080/blog/
http://localhost:8080/blog/1-and-here-the-slug

當我使用它時wget -r localhost:8080,它會創建以下結構:

.
├── static-files
│   └── ...
├── bar
├── blog
│   └── 1-and-here-the-slug
├── foo
└── index.html

barfoo1-and-here-the-slug是文件。我希望它們是包含單一檔案的目錄,命名index.html並且仍然不會破壞資源(CSS、JS 等)的路徑。

我期望這樣的事情:

.
├── static-files
│   └── ...
├── bar
│   └── index.html
├── foo
│   └── index.html
├── blog
│   ├── index.html // <---------- Also I want this one here to show the blog
│   └── 1-and-here-the-slug
│       └── index.html
└── index.html

我怎樣才能做到這一點?

答案1

http://localhost:8080/blog/1-and-here-the-slug

bar、foo 和 1-and-here-the-slug 是文件。我希望它們是包含單一檔案的目錄,名為index.html,仍然不會破壞資源(CSS、JS 等)的路徑。

├── blog
│   └── 1-and-here-the-slug
│       └── index.html

當您造訪http://localhost:8080/blog/1-and-here-the-slug目前目錄 is時blog,如果將該頁面重新命名為 be blog/1-and-here-the-slug/index.html,則新的目前目錄將為blog/1-and-here-the-slug。因此,您將破壞資源(CSS、JS)內的相對路徑(如果有)。和如果不修改文件的內部 HTML 就無法解決這個問題

您能做的最好的事情就是重命名沒有任何擴展名的檔案以具有 html 擴展名。

├── blog
│   └── 1-and-here-the-slug.html
  1. 保持相同的目錄,您可以rename遞歸地使用該命令:

前任:

  find tmp -type f ! -name '*.*' | rename -nv 's/(.*)/$1.html/'
  1. 您可以建立新目錄,但這會破壞相關資源(如果有)

前任:

  find tmp -type f ! -name '*.*' | while read file; do
       mv $file $file.tmp;
       mkdir $file;
       mv $file.tmp $file/index.html;
 done

您可以透過<base href="">在文件中插入標籤來指定資源的良好路徑來玩,但這將是一項艱鉅而昂貴的工作

  1. **或者更好,使用-Ewget 參數

編輯:閱讀wget手冊頁給你兩個很好的選擇

  -E
  --adjust-extension
       If a file of type application/xhtml+xml or text/html is downloaded
       and the URL does not end with the regexp \.[Hh][Tt][Mm][Ll]?, this option
       will cause the suffix .html to be appended to the local filename. 

  -k
   --convert-links
       After the download is complete, convert the links in the document to
       make them suitable for local viewing.  This affects not only the visible
       hyperlinks, but any part of the document that links to external content, 
       such as embedded images, links to style sheets, hyperlinks to non-
       HTML content, etc.

相關內容