C#判断网页中是否含有某字符串

 

简短的一段代码,可以判断webbrowser中打开的网页中是否含有某段字符串。这段代码比较有用,可以利用两个网页的不同之处来分辨出网页是否跳转到了需要的网页。

代码如下:转载请注明出处:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 判断网页中含有某个字符串
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.webBrowser1.Url = new Uri("http://www.baidu.com/");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string html = webBrowser1.Document.Body.OuterHtml;
            if (html.Contains("minxiangyangwfasg"))
            {
                this.label1.Text = "存在此字符串";
            }
            else
            {
                this.label1.Text = "不存在此字符串!";
            }
        }
    }
}

原文地址:https://www.cnblogs.com/minotmin/p/2699323.html