ブラウザでWebページを開くには、POSTパラメータを渡すにはどうすればよいですか?

ブラウザでWebページを開くには、POSTパラメータを渡すにはどうすればよいですか?

私たちはウェブサイトを開くことができます。得るブラウザのパラメータは次のとおりです。

#!/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-Agentlangおよびその他のヘッダーパラメータ)を制御できないため、他の方法が優れています。 Seleniumを使用している場合、ユーザーはUIオプションにのみバインドされ、これらの下位レベルのヘッダーは必要に応じて変更できません。


xdotoolTabこれは、ユーザーが特定のフォームフィールドに到達するのにかかる回数を計算し、コンテンツを入力する前に何度も繰り返す必要があるTabため、高価です。また、などの低レベルのPOSTパラメータを変更することもできませんUser-Agentlang

ベストアンサー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>&hellip;</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="&#34;Nominal Animal&#34;">
  </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='&amp;'
rawLt='<'    ; escLt='&lt;'
rawGt='>'    ; escGt='&gt;'
rawQuote='"' ; escQuote='&#34;'
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>&hellip;</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文字列操作を使用して各ブロックを、各&amp;with "&#34;<with &lt;、および各withに置き換えることで、クエリ文字列が隠し入力と呼ばれる属性に安全に書き込まれるようになります。 (この4つで十分です。後続の置換にはアンパサンドが含まれているため、最初にアンパサンドを実行することも重要です。>&gt;valuequery隠し入力の場合、クエリ文字列はURLにエンコードされません。単純なHTMLコンテンツですが、二重引用符はありません(値自体が二重引用符で囲まれているため)。 )

POSTリクエストの自動送信の欠点は、ウェブサイトがいつでもPOST変数の命名と内部URLを変更できるため、自動送信HTMLページを随時更新する必要があることです。

おすすめ記事