2

C#からInternet Explorerプロセスを開き、HTMLコンテンツをこのブラウザーに送信して、「表示された」コンテンツをキャプチャする方法はありますか?

他のhtmlストリッピングメソッド(HtmlAgilityPackなど)を知っていますが、上記の方法を調べたいと思います。

ありがとう、LG

4

2 に答える 2

3

WinFormsとWPFの両方に存在するWebBrowserコントロールを使用して、アプリケーションでIEをホストできます。次に、コントロールのソースをHTMLに設定し、コンテンツが読み込まれるのを待って(HTMLのダウンロードが完了したときに発生するLoadedイベントではなくLayoutUpdatedイベントを使用します。これは、必ずしも配置されておらず、すべての動的JSが実行されている必要はありません)、アクセスします。 HTMLを取得するためのDocumentプロパティ。

于 2012-02-19T14:52:10.630 に答える
0
    public List<LinkItem> getListOfLinksFromPage(string webpage)
    {
        WebClient w = new WebClient();
        List<LinkItem> list = new List<LinkItem>();
        try
        {
            string s = w.DownloadString(webpage);

            foreach (LinkItem i in LinkFinder.Find(s))
            {
                //Debug.WriteLine(i);
                //richTextBox1.AppendText(i.ToString() + "\n");
                list.Add(i);
            }
            listTest = list;
            return list;
        }
        catch (Exception e)
        {
            return list;
        }

    }

    public struct LinkItem
    {
        public string Href;
        public string Text;

        public override string ToString()
        {
            return Href;
        }
    }

    static class LinkFinder
    {
        public static List<LinkItem> Find(string file)
        {
            List<LinkItem> list = new List<LinkItem>();

            // 1.
            // Find all matches in file.
            MatchCollection m1 = Regex.Matches(file, @"(<a.*?>.*?</a>)", RegexOptions.Singleline);

            // 2.
            // Loop over each match.
            foreach (Match m in m1)
            {
                string value = m.Groups[1].Value;
                LinkItem i = new LinkItem();

                // 3.
                // Get href attribute.
                Match m2 = Regex.Match(value, @"href=\""(.*?)\""",
                RegexOptions.Singleline);
                if (m2.Success)
                {
                    i.Href = m2.Groups[1].Value;
                }

                // 4.
                // Remove inner tags from text.
                string t = Regex.Replace(value, @"\s*<.*?>\s*", "",
                RegexOptions.Singleline);
                i.Text = t;

                list.Add(i);
            }

            return list;

        }
    }

他の誰かが正規表現を作成したので、私はそれを信用できませんが、上記のコードは、渡されたWebページに対してWebclientオブジェクトを開き、正規表現を使用してそのページのすべてのchildLinkを検索します。これが探しているものかどうかはわかりませんが、そのHTMLコンテンツをすべて「取得」してファイルに保存したい場合は、「string s=w」の行に作成された文字列「s」を保存するだけです。 .DownloadString(webpage); " ファイルに。

于 2012-02-19T18:08:26.147 に答える