【UWSC】スクレイピングするには?サンプルコード有
今回は、UWSCで
ウェブサイトから情報を取得するコードを公表します。
例として、Twitterモーメント より、
3記事(タイトルと詳細URL)を取得・テキストファイル化します。
テキストの内容はこんな感じになります。
サンプルコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
//IE起動 Public IE IE = CreateOLEObj("InternetExplorer.Application") IE.Visible = True setOleEvent(IE) IE.Navigate("https://twitter.com/i/moments") //Twitterモーメント BusyWait(IE) sleep(1) body = IEGetSrc(IE,"body") //テキストファイル作成 FNAME = "C:\Users\〇〇\Desktop\moments.txt" DOSCMD("echo %DATE%%TIME% > " + FNAME) //日時を入力 ID = FOPEN(FNAME,F_READ or F_WRITE) //ループ(3記事を取得) FOR i = 1 TO 3 details = BetweenStr(body,"MomentCapsuleSummary-details","</div>",i) title = BetweenStr(details,"<a title=<#DBL>","<#DBL>") url = BetweenStr(details,"href=<#DBL>","<#DBL>>") DOSCMD("echo タイトル:" + title + ",URL:" +url + ">> " + FNAME)//タイトルとURLを入力 NEXT //IE終了 IE.Quit() //ファイルを保存して閉じる FCLOSE(ID) SLEEP(1) EXITEXIT // ここまで ---------------------------------------------- //------ DEF_DLL SendMessageA(Hwnd, Long, Long, Dword): bool: user32 CONST WM_ACTIVATE = $06 Public Pub_url, Pub_docc, Pub_BeforNav_cnt, Pub_DocComp_cnt, Pub_inp_flg Procedure BusyWait(IEObj) Sleep(0.3) tm = GetTime() repeat ifb GetTime() - tm > 90 // TimeOut Pub_docc = ""; Pub_BeforNav_cnt=0; Pub_DocComp_cnt=0 IEObj.Navigate(Pub_url) tm = GetTime() endif Sleep(0.2) ifb Pub_inp_flg Pub_inp_flg = False continue endif if IEObj.busy then continue if !IEObj.readyState=4 then continue if Pub_url = "" then break // No read if (Pos("*"+Pub_url+"*",Pub_docc)) or (Pos("*"+IEObj.LocationURL+"*",Pub_docc)) or (Pub_BeforNav_cnt<=Pub_DocComp_cnt) then else continue errflg = False if IEObj.document.title = "503 Unavailable" then errflg = True if IEObj.document.title = "Service Unavailable" then errflg = True if IEObj.document.title = "503 Service Temporarily Unavailable" then errflg = True ifb errflg Sleep(90) // 503: Wait&Retry tm = 0 continue endif Sleep(0.3) until ! Pub_inp_flg Pub_url=""; Pub_docc=""; Pub_BeforNav_cnt=0; Pub_DocComp_cnt=0 SendMessageA(IEObj.Hwnd, WM_ACTIVATE, 0, 0) Fend Procedure setOleEvent(IEObj) OleEvent(IEObj, "DWebBrowserEvents2", "BeforeNavigate2", "fucBeforNav") OleEvent(IEObj, "DWebBrowserEvents2", "DocumentComplete", "fucDocComp") OleEvent(IEObj, "DWebBrowserEvents2", "OnQuit", "fucQuit") Pub_url=""; Pub_docc=""; Pub_BeforNav_cnt=0; Pub_DocComp_cnt=0; Pub_inp_flg=False Fend Procedure fucBeforNav() ifb (Pos("javascript:",""+EVENT_PRM[1])=0) and ("about:blank"<>""+EVENT_PRM[1]) if Pub_url="" then Pub_url = ""+EVENT_PRM[1] Pub_BeforNav_cnt = Pub_BeforNav_cnt + 1 endif Pub_inp_flg = True Fend Procedure fucDocComp() ifb (Pos("javascript:",""+EVENT_PRM[1])=0) and ("about:blank"<>""+EVENT_PRM[1]) Pub_docc = Pub_docc + "*"+EVENT_PRM[1]+"*" Pub_DocComp_cnt = Pub_DocComp_cnt + 1 endif Pub_inp_flg = True Fend Procedure fucQuit() ExitExit Fend Procedure PopupWin(title, btn, no=-1, str="") sleep(1) GETALLWIN() id = Getid(title,"#32770",10) if no>=0 then Sendstr(id,str,no,True) Clkitem(id,btn) Fend |
変更箇所
以下を変更して、応用してみてくださいね。
1 2 3 4 5 6 7 8 9 |
IE.Navigate("https://twitter.com/i/moments") //取得したいサイトURL FNAME = "C:\Users\〇〇\Desktop\moments.txt" //保存先とファイル名 //ループ(取得する部分) FOR i = 1 TO 3 details = BetweenStr(body,"MomentCapsuleSummary-details","</div>",i) title = BetweenStr(details,"<a title=<#DBL>","<#DBL>") url = BetweenStr(details,"href=<#DBL>","<#DBL>>") DOSCMD("echo タイトル:" + title + ",URL:" +url + ">> " + FNAME)//タイトルとURLを入力 NEXT |
結果
まとめ
UWSCで、ウェブサイトから情報を取得する方法を見てみました。
Gmailを送信との相性が良いですね♪
ここでしか読めない!Web系Masakiさんへの質問
Web系エンジニアは必読。
ポートフォリオは、構想から約2ヶ月で完成したそう。
めっちゃかわいいの!
Twitterも要チェック☆
ほかにも50名以上のお話が読めるのは、このブログだけ!
ポートフォリオは、構想から約2ヶ月で完成したそう。
めっちゃかわいいの!
Twitterも要チェック☆
ほかにも50名以上のお話が読めるのは、このブログだけ!