如何获得一个网页的内容,在shell变量? [英] How to get the contents of a webpage in a shell variable?

查看:337
本文介绍了如何获得一个网页的内容,在shell变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux的我怎么能获取一个网址,并在shell脚本变量得到它的内容是什么?

In Linux how can I fetch an URL and get its contents in a variable in shell script?

推荐答案

您可以使用的wget 命令下载页面并读入一个变量:

You can use wget command to download the page and read it into a variable as:

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

我们使用的wget 这允许我们指定成<文件名的 -O 选项code>的wget 转储的页面内容。我们指定 - 来获得转储到标准输出和收集到变量内容。您可以添加 -q 静音选项关闭的wget的输出。

We use the -O option of wget which allows us to specify the name of the file into which wget dumps the page contents. We specify - to get the dump onto standard output and collect that into the variable content. You can add the -q quiet option to turn off's wget output.

您可以使用卷曲命令这个藏汉为:

You can use the curl command for this aswell as:

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

我们需要使用 -L 选项,我们要求可能已搬到了网页。在这种情况下,我们需要从新位置获得页面。在 -L - 位置选项有助于我们这个

We need to use the -L option as the page we are requesting might have moved. In which case we need to get the page from the new location. The -L or --location option helps us with this.

这篇关于如何获得一个网页的内容,在shell变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆