2
1

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.

C#でIEを立ち上げてGoogle検索をさせてみた

Last updated at Posted at 2019-08-25

注意:将来的にgoogleのhtmlソースが変わったら動かなくなります。

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;


class IeSample
{
    const int READYSTATE_COMPLETE = 4;
    
    static void GoogleSearchWithIE(string keyword)
    {
        Type comType = Type.GetTypeFromProgID("InternetExplorer.Application");
        dynamic ie = Activator.CreateInstance(comType);
        
        try {
            ie.Navigate("https://www.google.com/");
            
            while ( ie.Busy || ie.ReadyState < READYSTATE_COMPLETE ) {
                Thread.Sleep(50);
            }

            ie.Visible = true;

            dynamic html = ie.Document;
            
            dynamic t = html.getElementsByTagName("input");
            
            try {
                // Dump(t);
                
                if ( t["q"] == null ) {
                    Console.WriteLine("cannot find target element.");
                }
                else {
                    t["q"].Value = keyword;
                
                    dynamic form = html.forms("tsf");
                    if ( form == null ) {
                        Console.WriteLine("cannot find target form.");
                    }
                    else {
                        try {
                            form.submit();
                        }
                        finally {
                            Marshal.ReleaseComObject(form);
                        }
                    }
                }
            }
            finally {
                Marshal.ReleaseComObject(t);
            }
        }
        finally {
            Marshal.ReleaseComObject(ie);
        }
    }
    
    static void Dump(dynamic t)
    {
        for ( int i=0 ; i<t.Length ; i++ ) {
            Console.WriteLine("----");
            Console.WriteLine(t[i].Name??"");
            Console.WriteLine(t[i].Type??"");
            Console.WriteLine(t[i].Value??"");
        }
    }
    
    [STAThread]
    static void Main()
    {
        GoogleSearchWithIE("qiita");
    }
}

Googleのhtml抜粋

name="q"のinputタグが、検索ワードを入力するボックスで、
id="tsf"のformタグが、検索ボタンをもっています。

<form class="tsf nj" action="/search" style="overflow:visible" id="tsf" method="GET" name="f" onsubmit="return q.value!=''" role="search">...
<input class="gLFyf gsfi" maxlength="2048" name="q" type="text" ...>
2
1
4

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?