【问题标题】:wget `--reject-regex` not working?wget `--reject-regex` 不工作?
【发布时间】:2018-03-04 13:53:03
【问题描述】:

为什么下面的命令可以从www.example.com下载index.html

wget --reject-regex .* http://www.example.com/

$ wget --reject-regex .* http://www.example.com/
--2018-03-05 11:21:26--  http://.keystone_install_lock/
Resolving .keystone_install_lock... failed: nodename nor servname provided, or not known.
wget: unable to resolve host address ‘.keystone_install_lock’
--2018-03-05 11:21:26--  http://www.example.com/
Resolving www.example.com... 93.184.216.34
Connecting to www.example.com|93.184.216.34|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1270 (1.2K) [text/html]
Saving to: ‘index.html’

index.html                                                    100%[=================================================================================================================================================>]   1.24K  --.-KB/s    in 0s

2018-03-05 11:21:27 (4.49 MB/s) - ‘index.html’ saved [1270/1270]

FINISHED --2018-03-05 11:21:27--
Total wall clock time: 0.4s
Downloaded: 1 files, 1.2K in 0s (4.49 MB/s)

wget 的手册页说

--accept-regex urlregex

--reject-regex urlregex

指定一个正则表达式来接受或拒绝完整的 URL。

并且正则表达式.* 匹配所有内容。 (您可以使用freeformatter.com 进行验证)

我认为wget 下载的所有内容都会因为--reject-regex .* 选项而被拒绝。

.* 匹配 www.example.com,不是吗?

wget 为什么不忽略www.example.com 中的所有内容?

【问题讨论】:

    标签: regex download wget


    【解决方案1】:

    --regect-regex 只会拒绝 URL 链接,不会拒绝 index.html 中的标记文本。例如,如果网站包含指向 CSS 文件 main.css 的 URL,则此命令将递归下载网站但不包括 main.css

    wget -r --reject-regex 'main.css' www.somewebsite.com
    

    要忽略网站上的某些文本,请使用sed。举几个例子:

    # Ignores the word 'Sans'
    wget -qO- example.com | sed "s/Sans//g" > index.html
    
    # Ignores everything
    wget -qO- example.com | sed "s/.*//g" > index.html
    

    【讨论】:

    • 那么为什么www.example.com 没有被.* 拒绝呢? www.example.com 是一个 URL 链接,不是吗?
    • 因为--reject-regex '.*' 会拒绝www.example.com 中的所有URL。它不会拒绝www.example.com 中的所有文本。换句话说,--reject-regex 只拒绝给定网站中的 URL,而不是网站的实际文本。
    【解决方案2】:

    使用-np 选项拒绝索引文件。 --reject-regex 仅适用于递归文件(索引文件中的任何链接)。

       -np
       --no-parent
           Do not ever ascend to the parent directory when retrieving recursively.
           This is a useful option, since it guarantees that only the
           files below a certain hierarchy will be downloaded.
    

    【讨论】:

      【解决方案3】:

      部分答案是您的命令中的.* 可能已被您的shell 扩展为您当前工作目录中的匹配文件名列表,因为它没有用适当的引号括起来。您得到的输出中的.keystone_install_lock 可能是您当前工作目录中的文件名。 wget 在尝试连接到www.example.com 之前报告它。试试

      wget --reject-regex '.*' http://www.example.com/
      

      或者可能使用"" 而不是'',具体取决于您使用的shell。

      使用该命令我仍然可以检索到 index.html,所以我的答案并不完整。

      使用 Quantum7 建议的 -np,我仍然得到 index.html,所以这也不能完成答案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-08
        • 1970-01-01
        • 1970-01-01
        • 2019-11-19
        • 2020-07-24
        • 2019-01-24
        • 2011-09-25
        相关资源
        最近更新 更多