LoginSignup
0
0

More than 3 years have passed since last update.

cheerio-httpcli $(form-element).submitでactionを書き換える

Posted at

やりたいこと

formにactionが記載されておらずonclickのjavascriptでactionを書き換えるwebアプリをcheerio-httpcliでエミュレートする方法を説明する。

環境

cheerio-httpcli@0.7.4

cheerio-httpcliについて

Node.js用WEBスクレイピングモジュール。
htmlをパースしたり、web操作をエミュレートしたりできる。
https://github.com/ktty1220/cheerio-httpcli

cheerio-httpcliでエミュレートできるブラウザ操作は以下。「cheerioオブジェクトの独自拡張」
https://github.com/ktty1220/cheerio-httpcli#api%E7%9B%AE%E6%AC%A1

以下にも分かりやすくAPIの説明が乗っている。
https://qiita.com/ktty1220/items/64168e8d416d6d8ffb45

actionを書き換える方法

// documentInfo.urlをsubmitでpostしたいurlに書き換える
result.$.documentInfo().url = "https://google.com";
result.$("form[name=xxx]").submit();

formのaction属性が存在しない場合は、documentInfo.urlが使われる。詳細は以下参照。
https://github.com/ktty1220/cheerio-httpcli/blob/master/lib/cheerio/submit.js

あとがき

  • formのactionをjavascriptで書き換えるなんてほとんどないと思うが、古いアプリの操作で必要だったため。
  • cheerio-httpcliすごく使い易かった!今後もweb操作が必要になったら使っていきたい。
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