实现百度搜索页面网页遍历

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

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 在控件中打开网页
        /// </summary>
        public void baidu()
        {
            string url = "http://www.baidu.com/s?wd="+textBox1.Text;
            webBrowser1.Navigate(url.Trim());
        }
        private void button1_Click(object sender, EventArgs e)
        {
            baidu();
            
        }
        /// <summary>
        /// 打开新网页不会跳转到其他浏览器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            webBrowser1.Navigate(webBrowser1.StatusText);
        }

        /// <summary>
        /// 获取网页所有节点,遍历所有节点,如有标签的文本值是"下一页",模拟点击,进入下一页
        /// </summary>
        public void bianli()
        {
            IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
            foreach (IHTMLElement ele in doc.all)
            {
                if (ele.innerText == "下一页>")
                {                   
                    ele.click();
                    break;
                }
            }
        }

        /// <summary>
        /// 判读网页是否加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (e.Url==webBrowser1.Document.Url)
            {
                bianliwangye();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //bianli();
            bianliwangye();
        }

        /// <summary>
        /// 枚举获取百度搜索页面的所有网址
        /// </summary>
        public void bianliwangye()
        {            
            IHTMLDocument2 document = (IHTMLDocument2)webBrowser1.Document.DomDocument;//获取源代码
            IHTMLElementCollection hc = (IHTMLElementCollection)document.all;//获取所有标签
            //MessageBox.Show(hc.ToString());
            foreach (IHTMLElement h in hc)//遍历标签
            {
                if (h.className=="g"||h.className=="c-showurl")//以标签classname判读
                {
                    string a= h.innerHTML;//获取标签文本内容
                    if (a.Contains("&nbsp"))
                    {
                        int b = a.IndexOf("&nbsp");
                        string a1 = a.Substring(0, b);//截取网址
                        MessageBox.Show(a1);
                    }                                      
                }
            }
            bianli();//当枚举到当前页面最后一个网址,模拟点击进入下一页           
        }
    }
}

 

转载于:https://www.cnblogs.com/happinesshappy/p/4596297.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值