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?

More than 5 years have passed since last update.

JScriptをVBAの関数にしてRSSの代わりをさせてYahoo、Bingの記事をヘッドラインをWord、テキスト化する

Last updated at Posted at 2017-02-11

Use VBA with Use Fu1nction Jscript (JSE) and Alter RSS, Get text from yahoo, bing and Insert word document file or make utf-8 text file (Windows 10 Enable, office 2013 late 64bit enable)

注意点

今回のプログラムはHPのデザインが変わると使えません。そのたびに解析する必要があります。
また新聞サイトはログインしてやっているので、ログインできない場合はわかりません。

今回はこちらを参考に

たった5行のコードでWebページ取得・解析をするJavaScriptプログラム

これを応用します。しかしこのJscriptはIEが無限にたまってしまうようです。

無限にIEが起動するのを防ぐために

IE.Quit();
IE = null;
この2行を付け加えます。

まずYahoo用の関数です

変数を代入するにはArrayを使います。
つまり変数を並べればいいようです。
WSH.ECHO(yahoointl(args[0]));
今回は0で決め打ちしています。
配列は0からスタートするからです。

Functionyahoointl.jse

var args = new Array();
var oArgs = WScript.Arguments;
try{
for (var tmpI = 0; tmpI < oArgs.length; tmpI++) {
args[tmpI] = oArgs(tmpI);
}
}catch(err){
wscript.quit();
}

oArgs = null;
WSH.ECHO(yahoointl(args[0]));
function yahoointl(ss){
try{
var IE = new ActiveXObject('InternetExplorer.Application');
IE.navigate(ss);
while (IE.busy);
while (IE.document.readyState != "complete");
var buf = IE.document.getElementById('main').innerText;
IE.Quit();
IE = null;
}catch(err){
wscript.quit();
}
return buf;
}

Word側

文書に文字列を挿入する
VBAに入れるとき、参照設定でWindows Script Hostに参照設定をしてください。
次に先ほどのファイルFunctionyahoointl.jseをドキュメントフォルダにおいたものとして処理しています。
strCMDEXE = " C:\Windows\System32\CScript.exe "
は32BitのCscriptで動くようにしています。
 この方法の欠点はCMD.EXEの画面が開くことです。
 それでも64BitオフィスでJscriptを使うにはこの方法しかないので使います。

InsertYahoointl.vb
Sub InsertYahoointl()
'Jscriptを呼び出して、yahoojp国際のテキストをドキュメントに読み込む
Dim ar
Dim WSHR As New IWshRuntimeLibrary.WshShell
Dim wDoc: Set wDoc = ThisDocument
wDoc.Select
Selection.Delete
Dim str, funcstr, sURL
funcstr = "Functionyahoointl.jse "
str = Environ("USERPROFILE") & "\Documents\" & funcstr
sURL = "http://news.yahoo.co.jp/hl?c=c_int"
ar = WSHR.Exec("C:\Windows\System32\CScript.exe " & str & sURL).StdOut.ReadAll
ActiveDocument.Content.InsertAfter Text:=CStr(ar)
End Sub

テキスト化:Bingを使って特定のキーワードのニュースをテキストファイルにする

参考 JScript・WSHで、UTF-8でファイルの読み書きをする方法

キーワードをとりあえずトランプ大統領としてBingでニュースを検索します。
次に絞り込み条件を7日以内にします。
こうして特定のキーワードで7日以内のニュースを検索するための文字列ができます。
これをADODBでWriteし、
"D:\sample.txt"
に上書きします。

Bing.Jse
 /* ADODB.Stream - SaveOptionsEnum */
   var adSaveCreateOverWrite = 2;
  /* ADODB.Stream - StreamTypeEnum */
    var adTypeBinary = 1;
    var adTypeText = 2;
 /* ADODB.Stream - StreamWriteEnum */
    var adWriteChar = 0;
    var adWriteLine = 1;
    var IE = new ActiveXObject('InternetExplorer.Application');
    IE.navigate('http://www.bing.com/news/search?q=%e3%83%88%e3%83%a9%e3%83%b3%e3%83%97%e5%a4%a7%e7%b5%b1%e9%a0%98&qft=interval%3d%228%22&form=PTFTNR');
while (IE.busy);
while (IE.document.readyState != "complete");
var buf = IE.document.getElementById('mainid').innerText
WSH.Echo( buf );
         var st = new ActiveXObject("ADODB.Stream");
         st.type = adTypeText;
         st.charset = "utf-8";
         st.open();
         st.WriteText( buf , adWriteLine );
         st.SaveToFile("D:\sample.txt",adSaveCreateOverWrite);
         st.Close();
st = null;
IE.Quit();
IE = null;

作成のポイント

ポイントはなんといってもNavigateのURLとgetElementByIdに何を入れるかというところになります。あとちょっと不安定で、同じWin10マシンでも動いたり動かなかったりします。

IEで該当ページを開き、F12を押してソースを開きます。
これで取り込みたいテキストの場所を探してこのIDを決めていきます。

長所

ポイントはブラウザを開くのにイライラさせられることがなく、ポンポンとJSEを起動させ、裏で情報が収集できることだと思います。
RSSが使えないところでは便利ではないでしょうか。
なおBingのニュースサイトを使っているのは、裏で起動しているIEをリスペクトするためです。MicroSoftのBingを使ったのはここが便利とかそういうことではありません。

短所

窓が開くこともそうですが、読み込むことに失敗するとIEが起動したまま詰みあがります。原因が不明ですが、システムが不安定になり再起動が必要となります。見よう見まねでTry catchでエラーで止まるようにしたので改善するでしょうか。

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?