一键浏览

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CefSharp.WinForms;
using CefSharp;
using System.Web;
using HtmlAgilityPack;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace BSSimple
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void buttonKanKan_Click(object sender, EventArgs e)
        {
            HtmlElement searchWords = webBrowser1.Document.All["kw"];//获取百度搜索的文本框  <br>
            HtmlElement searchButton = webBrowser1.Document.All["su"];//获取百度搜索的按钮  <br>
            searchWords.SetAttribute("value", this.Key.Text.Trim());//给百度搜索的文本框赋值  <br>
            searchButton.InvokeMember("click");//调用百度搜索按钮的点击事件 

        }
        public void HideScriptErrors(WebBrowser wb, bool Hide)
        {
            FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
            if (fiComWebBrowser == null)
                return;

            object objComWebBrowser = fiComWebBrowser.GetValue(wb);

            if (objComWebBrowser == null)
                return;

            objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide });

        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            this.AcceptButton = buttonKanKan;
            this.webBrowser1.ScriptErrorsSuppressed = true;
            this.webBrowser1.Navigate("http://www.baidu.com");
        }

        private void webBrowser1_Navigated()
        {


            HideScriptErrors(webBrowser1, true);
        }
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            if (!e.Url.Equals(webBrowser1.Url))
            {
                //not actually complete, do nothing
                return;
            }
            HideScriptErrors(webBrowser1, true);
            if (this.webBrowser1.Url.ToString() != "https://www.baidu.com/")
            {
                HtmlElement target = webBrowser1.Document.GetElementById("content_left");
                Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>");
                MatchCollection mc = reg.Matches(target.InnerHtml);
                string url = string.Empty;
                foreach (Match m in mc)
                {
                    url = m.Groups["url"].Value;
                    if (url.StartsWith("http"))
                    {
                        break;
                    }
                }
                System.Diagnostics.Process.Start(url);
            }
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值