0
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?

複数ページ構成のweb記事の次のページの url を作る

0
Last updated at Posted at 2026-01-31

こんにちは。
複数ページ構成のweb記事に対して、その url の次のページの url を作るシェルスクリプトを作りました。ただし万全ではないと思うのでご注意願います。

$ ./next_page.sh https://gendai.media/articles/-/163037
https://gendai.media/articles/-/163037?page=2
next_page.sh
#!/bin/sh

url="$1"
prog_name=$(basename "$0")

# functions
print_usage_f() {
cat << EOS
usage:
  $prog_name https://gendai.media/articles/-/163037
  $prog_name https://ascii.jp/elem/000/004/366/4366125
  $prog_name https://natgeo.nikkeibp.co.jp/atcl/web/15/403964/011400177
  $prog_name https://www.newsweekjapan.jp/kimura/2026/01/post-343.php
EOS
}

page_num_from_param_f() {
    url="$1"
    n=$(echo "$url" | grep -oE '[?&](page|P)=[0-9]+' | grep -oE '[0-9]+')
    [ -n "$n" ] || n=1
    echo "$n" 
}

page_num_from_file_f() {
    url="$1"
    n=1
    echo "$url" | grep -oq '_' && n=$(echo "$url" | sed 's/.*_\([0-9]*\)\..*/\1/')
    echo "$n" 
}

url_from_file_f() {
  url="$1"
  page_num=$2
  file="${url##*/}"
  base="${file%%.*}"; base="${base%%_*}"
  ext="${file##*.}"
  echo "${url%/*}/${base}_${page_num}.$ext"
}

page_num_from_subdir_f() {
  url="$1"
  n=$(echo "${url%/}" | grep -oE '[0-9]+$')
  { [ -z "$n" ] || [ "$n" -gt 12 ]; } && n=1
  echo "$n"
}

url_from_subdir_f() {
  url="$1"
  page_num=$2
  base="${url%/}"
  n=$(echo "$base" | grep -oE '[0-9]+$')
  [ "$n" -le 12 ] && base=${base%/*}
  echo "$base/${page_num}/"
}

page_num_f() {
  url="$1"
  case "$url" in
    *newsweekjapan.jp*)
      page_num_from_file_f "$url" ;;
    *ascii.jp*)
      page_num_from_subdir_f "$url" ;;
    *nikkeibp.co.jp*)
      page_num_from_param_f "$url" ;;
    *gendai.media*)
      page_num_from_param_f "$url" ;;
  esac
}

url_f() {
  url="$1"
  page_num=$2
  case "$url" in
    *newsweekjapan.jp*)
      url_from_file_f "$url" "$page_num" ;;
    *ascii.jp*)
      url_from_subdir_f "$url" "$page_num" ;;
    *nikkeibp.co.jp*)
      echo "${url%%\?*}?P=${page_num}" ;;
    *gendai.media*)
      echo "${url%%\?*}?page=${page_num}" ;;
  esac
}

# usage
case "$1" in -h|--help) print_usage_f; exit;; esac

# main
n=$(page_num_f "$url")
url_f "$url" $((n + 1))
0
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
0
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?