2
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 3 years have passed since last update.

Instagramの画像を簡単に保存する方法 VBscriptでInstagramのWEBスクレイピングした話

Last updated at Posted at 2018-01-10

インスタグラムの仕様変更で非ログインでは取得できなくなりました(20201201)

VBscriptでInstagramのWEBスクレイピングした話

Instagramの画像を簡単に保存する方法

目的

Instagramの画像を扱いやすい形式にしてlocalhostで参照する

経緯

Instagramの画像を保存したい

Instagramの画像はdivタグでマスクされていて右クリックで簡単に保存したりできないようになっている

画像を保存したければF12キーを押して開発者ツールを立ち上げるか外部サービスにURLを貼り付けるのが主流

これは面倒くさい

WEBで公開されているものだしウェブスクレイピング(Web scraping)します

環境

言語:VBScript
サーバー:Windows IIS ASP

ソース

全体はgithub参照くださいhttps://github.com/wagase/VBscraping/tree/master/Instagram
個別解説

WEBサイトへアクセス

Private Function getXMLHTTP(byval url)

	Dim xmlhttp,resText
	Set xmlhttp = Server.Createobject("MSXML2.ServerXMLHTTP")
	xmlhttp.Open "GET",url, False
	xmlhttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
	xmlhttp.Send ""
	resText = xmlhttp.responseText
	Set xmlhttp = Nothing

	getXMLHTTP = resText
End Function`

VBSでWEBサイトへアクセスするには
Server.Createobject("MSXML2.ServerXMLHTTP")
を使います
xmlhttp.responseTextで結果が(この場合HTMLが)返ってくるのでそれを解析します

HTMLの解析

実際にだれかのInstagramにアクセスしてソースをみるとscriptタグでjavascriptの記述があることがわかります。これをテキスト形式で取得することになるので正規表現で解析します。
こんな感じで
"https://scontent-nrt1-1\.cdninstagram\.com.*?\.jpg"
取得したものをHTMLで<img src=~~と書き直してやれば画像を取るだけなら終了です

動画もほしい

動画をとるには工夫が必要でInstagramの記事固有ページに行く必要がありました

記事固有ページへは上記のjavascriptでcodeと書かれている11桁の英数字の文字列を
https://www.instagram.com/p/(11桁の英数字)
のようにすると行けるようです
したがってまずは個別ページのcodeを取得します
正規表現で
"""code"":.*?"", ""date"""
とかきMid関数で10文字目から11文字抜くと良さそうです

Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = """code"":.*?"", ""date"""
regEx.IgnoreCase = False ' 大文字と小文字を区別しない
regEx.Global = True ' 文字列全体を検索

あとは
Set matches = regEx.Execute()
を使って順番に個別ページにアクセスを行いそれぞれ結果を取得して
画像なら
<img src="(画像URL)">
動画なら
<video autoplay loop muted controls><source src="(動画URL)" type=""video/mp4"" /></video>
と書いてresponse.Writeしてやるとマスクのかかっていない画像の一覧が取得できます

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