1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HARファイルの内容から指定URLの内容のみを抽出する方法

Posted at

開発者ツールのネットワークデータで、[Preserve log][持続]モードで一連のページ遷移の流れを記録したときに、HARファイル(Chromeの開発者ツールやFireFoxのFireBugなどのネットワークログをダンプしたファイル)の内容が大きくなってしまいます。
これは、表示したページのHTML以外にも画像やcssなど全てのリクエスト内容が含まれるためです。
しかし、分析や調査のときには着目したいリクエスト(特定のURL)の分があれば十分です。

そこで、jqコマンド(v1.4)を使ってHARファイルから指定URLの内容のみを抽出するスクリプトを作りました。

このコマンドで抽出した内容はHARファイルの構造をそのまま保っているので、HARファイルビューアなどでも問題なく閲覧できます。

# !/bin/bash
#
# harファイルのログデータで、指定のURLのアクセスのみを抽出する
#
# Requie:
#  jq v1.4
#
# Example:
#  1) har_filter "http://example.com/hoge" har_file.har
#  2) cat har_file.har | har_filter "http://example.com/hoge"
#
set -e

filter_url=$1
har_file=${2:--}

contents=$(cat $har_file | \
jq -r "$(cat <<EOS
.log | with_entries(if .key != "entries" then
 .
else
 .value |= map(select(.request.url | startswith("$filter_url")))
end)
EOS
)"
)

cat <<EOS | jq .
{ "log": $contents }
EOS

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?