今回は、フォロアー数を読み込んでcsvファイルに追記するところまで
1.Automatorで指定されたテキストを取得
1)テキストボックスに微博のURLを書き込む
2)テキストからURLを取り出す
3)Applescriptへ送る
ファイルから取得する場合は下記のスプリクトで
on run {input, parameters} set theText to read (choose file) return theText end run
2.ページソースを保存する
1)URLテキストをパラメータにしてApple Scriptで実装
微博のページを開いてページのソースをHTMLファイルに書き出す
2)スクリプト詳細
(1)コード
on run {input, parameters} set target_URL to item 1 of input -- item 1 of input がURLテキストになる my openWithWait(target_URL, 15) -- time out 15secでtarget_URLが開くのを待つ if result is true then tell application "System Events" -- ソースを保存する処理 tell application "Safari" activate set URL of document 1 to target_URL set myString to source of document 1 -- myStringにページソースを読み込む end tell set newFile to POSIX file "/Users/luke24e-hb/Documents/myfiles/BLOG/W100_fanschart/fans.html" open for access newFile with write permission -- 保存するファイルを開いて write myString as «class utf8» to newFile -- 読み込んだページソースをUTF-8で保存 close access newFile end tell end if end run -- open `theUrl` and check to complete page loading -- timeoutSec: timeout(sec) -- Return true if success on openWithWait(theUrl, timeoutSec) tell application "Safari" open location "about:blank" -- 最初にブランクページを開く -- display dialog URL of document 1 as text activate delay 0.5 do JavaScript "location.href=\"" & theUrl & "\";" in document 1 -- target_URLを開く repeat timeoutSec times -- Time Outまで繰り返し delay 1 if ((URL of document 1 as text) does not start with "about:") then set state to do JavaScript "document.readyState" in document 1 if (state = "complete") then return true --- target_URLの文書と全ての副リソースの読み込みが完了したらtrue end if end repeat return false --- Time Outでfalse end tell end openWithWait
4.フォロアー数を抜き取りcsvファイルに追記
1)シェルスクリプト(bash)で実装
2)スクリプト詳細
(1)コード
file_html='/Users/luke24e-hb/Documents/myfiles/BLOG/W100_fanschart/fans.html' file_csv='/Users/luke24e-hb/Documents/myfiles/BLOG/W100_fanschart/fans.csv' fans=`awk '{print substr($0, index($0, "relate=fans"), index($0, "a") -1 )}' $file_html | grep "relate=fan" | grep -v "from" | awk -F "[¥(¥)]" '{print $3}'` delimiter=',' datetime=`date "+%Y/%m/%d %H:%M:%S"` if [[ -f $file_html ]]; then echo $datetime$delimiter$fans >> $file_csv rm $file_html echo $datetime$delimiter$fans else echo "Time Over" fi
(2)内容メモ
①HTMLファイルの中身は
href=\"\/p\/1004063279873201\/follow?relate=fans\" >她的粉丝(1031776)<\/a><
この部分を抽出して1031776を取り出す
②shell詳細
(1)抽出Step1:HTMLファイル"relate=fans"から"a"の間を抽出して行に分割
awk '{print substr($0, index($0, "relate=fans"), index($0, "a") -1 )}' $file_html
(2)抽出Step2:relate=fans"がなくても"a"があると抽出されるのでさらに"relate=fan"のある行を抽出
grep "relate=fan"
(3)抽出Step3:2か所あるので"from"のある行を除外して抽出する行を特定
grep -v "from"
(4)抽出Step4:カッコを区切り文字に設定して囲まれた数を抽出
awk -F "[¥(¥)]" '{print $3}'
(5)タイムスタンプを追加する:フォ-マットは 2013/02/08 09:30:26
date "+%Y/%m/%d %H:%M:%S"
(6)HTMLファイルがあればcsvファイルにコンマ区切りで追記、結果表示用にも出力
(7)HTMLファイルがなければ Time Overを出力
(8)HTMLファイルは消去
5.結果を表示する
タイムアウトを付けてアラートで表示
6.実行ファイルパーミッション設定
セキュリティとプライバシー>プライバシーでアプリケーションにコンピュータの制御許可を忘れずに設定
完成したcsvファイルはこんな感じ 地道に増えてる
2019/10/19 11:41:11,1031874
2019/10/19 11:56:19,1031881
2019/10/19 11:57:38,1031881
2019/10/19 12:04:52,1031888
2019/10/19 13:08:12,1031929
2019/10/19 13:38:27,1031949
2019/10/19 13:44:09,1031950
2019/10/19 13:54:54,1031952
2019/10/19 13:58:27,1031953
2019/10/19 14:00:03,1031954
Webアプリについてこれまであまり触ったことがなく多くのページを参考にさせていただきました
感謝
主要参照先は、整理して最後のページに掲載します
part03へ続く
【2019/12/12 python版追加】
luke24e-2h.hatenablog.com
【2020/2/22】
python版へ移行したため今後のApplescript版フォローなし