代码之家  ›  专栏  ›  技术社区  ›  Aillyn

如何在shell变量中获取网页的内容?

  •  95
  • Aillyn  · 技术社区  · 15 年前

    6 回复  |  直到 15 年前
        1
  •  197
  •   codaddict    15 年前

    wget

    content=$(wget google.com -q -O -)
    echo $content
    

    我们使用 -O 选择 它允许我们指定文件的名称 wget公司 - 将转储到标准输出并将其收集到变量中 content . 您可以添加 -q

    你可以用 curl 命令以及:

    content=$(curl -L google.com)
    echo $content
    

    -L 选项,因为我们请求的页面可能已移动。在这种情况下,我们需要从新位置获取页面。这个 -我 --location

        2
  •  29
  •   Benjamin W.    7 年前

    有很多方法可以从命令行获取页面。。。但这也取决于您想要的是源代码还是页面本身:

    curl $url
    

    使用wget:

    wget -O - $url
    

    lynx -dump $url
    

    我认为你可以找到这么多解决这个小问题的方法,也许你应该阅读所有这些命令的手册页。别忘了更换 $url 通过您的URL:)

    祝你好运:)

        3
  •  9
  •   Colin Hebert    15 年前

    这就是 wget 命令或 curl .

    现在可以使用wget下载的文件。或者你可以用卷曲来处理一条流。


    资源:

        4
  •  3
  •   Jim Lewis    15 年前
    content=`wget -O - $url`
    
        5
  •  3
  •   Giacomo    15 年前

    你可以用 curl wget 检索原始数据,或者您可以使用 w3m -dump

    $ foo=$(w3m -dump http://www.example.com/); echo $foo
    You have reached this web page by typing "example.com", "example.net","example.org" or "example.edu" into your web browser. These domain names are reserved for use in documentation and are not available for registration. See RFC 2606, Section 3.
    
        6
  •  3
  •   ephemient    15 年前

    如果你有 LWP 安装后,它提供了一个简单命名为 GET ".

    $ GET http://example.com
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <HTML>
    <HEAD>
      <META http-equiv="Content-Type" content="text/html; charset=utf-8">
      <TITLE>Example Web Page</TITLE>
    </HEAD> 
    <body>  
    <p>You have reached this web page by typing &quot;example.com&quot;,
    &quot;example.net&quot;,&quot;example.org&quot
      or &quot;example.edu&quot; into your web browser.</p>
    <p>These domain names are reserved for use in documentation and are not available 
      for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC 
      2606</a>, Section 3.</p>
    </BODY>
    </HTML>
    

    wget -O- , curl ,和 lynx -source

        7
  •  0
  •   Itchydon Sergei Zinovyev    5 年前

    没有卷发,没有wget,没有ncat,什么都没有?使用 telnet :

    $ content=$(telnet localhost 80)
    GET / HTTP/1.1
    Host: localhost
    Connection: close
     
    Connection closed by foreign host.
    

    $ echo $content
    HTTP/1.1 200 OK Date: Mon, 22 Mar 2021 12:45:02 GMT Server:
    Apache/2.4.46 (Fedora) OpenSSL/1.1.1j Last-Modified: Mon, 31 Dec 2018
    15:56:45 GMT ETag: "a4-57e5375ad21bd" Accept-Ranges: bytes
    Content-Length: 164 Connection: close Content-Type: text/html;
    charset=UTF-8 Success! 192.168.1.1
    
    推荐文章