私たちはウェブサイトを開くことができます。得るブラウザのパラメータは次のとおりです。
#!/bin/bash
echo 'enter username'
read username
firefox "https://github.com/${username}"
これで、1つのコマンドですべてのユーザーのgithubページにアクセスし、ユーザー名を入力できるので便利です。同様に、パラメータに渡されたクエリを使用してGoogleを検索するシェルスクリプトを作成できます。
必要なウェブサイトを開く方法郵便はがき端末から直接ウェブサイトにアクセスできるようにパラメータを渡したいですか?
例えば、https://www.startpage.com。 POST要求を転送できる場合は、端末から直接クエリを取得できます。
注:データを取得するためのカールベースの回答を見つけるのではなく、WebサイトにアクセスするためのFirefoxまたは他のブラウザベースの回答を見つけることです。
Selenium
ユーザーはPOST要求に渡される低レベルのデータ(たとえばUser-Agent
、lang
およびその他のヘッダーパラメータ)を制御できないため、他の方法が優れています。 Seleniumを使用している場合、ユーザーはUIオプションにのみバインドされ、これらの下位レベルのヘッダーは必要に応じて変更できません。
xdotool
Tabこれは、ユーザーが特定のフォームフィールドに到達するのにかかる回数を計算し、コンテンツを入力する前に何度も繰り返す必要があるTabため、高価です。また、などの低レベルのPOSTパラメータを変更することもできませんUser-Agent
。lang
ベストアンサー1
一時自動送信 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文字列操作を使用して各ブロックを、各&
with "
、"
各<
with <
、および各withに置き換えることで、クエリ文字列が隠し入力と呼ばれる属性に安全に書き込まれるようになります。 (この4つで十分です。後続の置換にはアンパサンドが含まれているため、最初にアンパサンドを実行することも重要です。>
>
value
query
値隠し入力の場合、クエリ文字列はURLにエンコードされません。単純なHTMLコンテンツですが、二重引用符はありません(値自体が二重引用符で囲まれているため)。 )
POSTリクエストの自動送信の欠点は、ウェブサイトがいつでもPOST変数の命名と内部URLを変更できるため、自動送信HTMLページを随時更新する必要があることです。