我们可以在浏览器中使用GET参数打开一个网站,如下所示
#!/bin/bash
echo 'enter username'
read username
firefox "https://github.com/${username}"
这很方便,因为我现在只需一个命令就可以访问任何用户的github页面,然后输入他们的用户名。同样,我们可以制作一个shell脚本,以通过参数中传递的查询来搜索Google。
如何打开一个需要传递POST参数的网站,以便可以从终端直接访问该网站?
以https://www.startpage.com为例。如果可以传递POST请求,那么我们可以直接从终端搜索查询。
注意:不是基于curl来寻找答案来检索数据,而是基于firefox或任何其他浏览器来访问网站的答案。
任何其他方式优于Selenium
过像POST请求传递低水平的数据,因为用户将无法控制User-Agent
,lang
以及其他一些头参数。如果使用Selenium,则用户只能绑定到UI选项,并且这些低级标题不能根据需要进行修改。
xdotool
这将是昂贵的,因为用户必须计算要执行Tab的次数才能到达特定的表单字段,然后Tab在该处键入内容之前要循环多次。它也没有给我改低级别POST参数的能力等User-Agent
,lang
等
您创建了一个自动提交的临时HTML页面,将浏览器指向该页面,几秒钟后,您删除了该临时HTML文件,因为不再需要它了。以脚本形式:
#!/bin/bash
# Create an autodeleted temporary directory.
Work="$(mktemp -d)" || exit 1
trap "cd / ; rm -rf '$Work'" EXIT
# Create a HTML page with the POST data fields,
# and have it auto-submit when the page loads.
cat > "$Work/load.html" <<END
<!DOCTYPE html>
<html>
<head>
<title>…</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function dosubmit() { document.forms[0].submit(); }
</script>
</head>
<body onload="dosubmit();">
<form action="https://www.startpage.com/do/asearch" method="POST" accept-charset="utf-8">
<input type="hidden" name="cat" value="web">
<input type="hidden" name="cmd" value="process_search">
<input type="hidden" name="language" value="english">
<input type="hidden" name="engine0" value="v1all">
<input type="hidden" name="query" value=""Nominal Animal"">
</form>
</body>
</html>
END
# Load the generated file in the browser.
firefox "file://$Work/load.html"
# Firefox returns immediately, so we want to give it a couple
# of seconds to actually read the page we generated,
# before we exit (and our page vanishes).
sleep 2
让我们更改上面的内容,以便我们对命令行上提供的任何字符串进行StartPage搜索:
#!/bin/bash
# Create an autodeleted temporary directory.
Work="$(mktemp -d)" || exit 1
trap "cd / ; rm -rf '$Work'" EXIT
# Convert all command-line attributes to a single query,
# escaping the important characters.
rawAmp='&' ; escAmp='&'
rawLt='<' ; escLt='<'
rawGt='>' ; escGt='>'
rawQuote='"' ; escQuote='"'
QUERY="$*"
QUERY="${QUERY//$rawAmp/$escAmp}"
QUERY="${QUERY//$rawQuote/$escQuote}"
QUERY="${QUERY//$rawLt/$escLt}"
QUERY="${QUERY//$rawGt/$escGt}"
# Create a HTML page with the POST data fields,
# and have it auto-submit when the page loads.
cat > "$Work/load.html" <<END
<!DOCTYPE html>
<html>
<head>
<title>…</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function dosubmit() { document.forms[0].submit(); }
</script>
</head>
<body onload="dosubmit();">
<form action="https://www.startpage.com/do/asearch" method="POST" accept-charset="utf-8">
<input type="hidden" name="cat" value="web">
<input type="hidden" name="cmd" value="process_search">
<input type="hidden" name="language" value="english">
<input type="hidden" name="engine0" value="v1all">
<input type="hidden" name="query" value="$QUERY">
</form>
</body>
</html>
END
# Load the generated file in the browser.
firefox "file://$Work/load.html"
# Firefox returns immediately, so we want to give it a couple
# of seconds to actually read the page we generated,
# before we exit (and our page vanishes).
sleep 2
所有这一切的变化是我们使用的Bash字符串操作来代替每个块&
用&
,每次"
用"
,每次<
用<
,每个>
用>
,这样的查询字符串可以安全地写成value
名为隐藏输入的属性query
。(这四个就足够了。首先做“&”号也很重要,因为后续的替换都包含“&”号。由于我们将其作为隐藏输入的值发出,因此查询字符串不是经过url编码的;它只是普通的HTML内容,但是不带双引号(因为值本身是双引号)。
自动提交POST请求的不利之处在于,您可能需要不时地更新自动提交的HTML页面,这仅仅是因为该站点可以随时更改POST变量的名称和内部URL。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句