【发布时间】:2010-09-18 18:44:42
【问题描述】:
在 Linux 中,如何获取 URL 并在 shell 脚本的变量中获取其内容?
【问题讨论】:
在 Linux 中,如何获取 URL 并在 shell 脚本的变量中获取其内容?
【问题讨论】:
您可以使用wget 命令下载页面并将其读入变量中:
content=$(wget google.com -q -O -)
echo $content
我们使用wget 的-O 选项,它允许我们指定wget 转储页面内容的文件的名称。我们指定- 将转储到标准输出并将其收集到变量content 中。您可以添加-q quiet 选项来关闭 wget 输出。
您可以为此使用curl 命令以及:
content=$(curl -L google.com)
echo $content
我们需要使用-L 选项,因为我们请求的页面可能已经移动。在这种情况下,我们需要从新位置获取页面。 -L 或 --location 选项可以帮助我们解决这个问题。
【讨论】:
src 属性,然后下载那个页面。如果你安装了tq,这个命令应该可以做到:curl -s http://ww1.watchop.io/manga2/read/one-piece/1/4 | tq -j -a src "#imgholder a img" | xargs wget
convert_links = on 和 -O- 选项。它失败并出现错误-k can be used together with -O only if outputting to a regular file.。是预期的吗?
从命令行获取页面的方法有很多种……但这也取决于您需要代码源还是页面本身:
如需代码源:
卷曲:
curl $url
使用 wget:
wget -O - $url
但如果您想获得通过浏览器可以看到的内容,lynx 会很有用:
lynx -dump $url
我认为你可以为这个小问题找到很多解决方案,也许你应该阅读这些命令的所有手册页。并且不要忘记用您的 URL 替换 $url :)
祝你好运:)
【讨论】:
【讨论】:
content=`wget -O - $url`
【讨论】:
您可以使用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.
【讨论】:
没有 curl,没有 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
【讨论】: