我们可以在浏览器中打开一个带有GET参数的网站,如下
#!/bin/bash
echo 'enter username'
read username
firefox "https://github.com/${username}"
Run Code Online (Sandbox Code Playgroud)
这很方便,因为我现在可以通过一个命令访问任何用户的 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
Run Code Online (Sandbox Code Playgroud)
让我们更改上面的内容,以便我们对命令行上提供的任何字符串进行 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
Run Code Online (Sandbox Code Playgroud)
所有改变的是我们使用 Bash 字符串操作替换每个&with &、每个"with "、每个<with<和每个>with 的块>,这样查询字符串就可以安全地写为value名为 的隐藏输入的属性query。(这四个就足够了。首先做与号也很重要,因为后续的替换包含与号。由于我们将其作为隐藏输入的值发出,因此查询字符串不是 url 编码的;它只是普通的 HTML 内容,但是没有双引号(因为值本身是双引号)。
自动提交 POST 请求的缺点是您可能需要不时更新自动提交的 HTML 页面,因为站点可以随时更改 POST 变量命名和内部 URL。
| 归档时间: |
|
| 查看次数: |
14818 次 |
| 最近记录: |