马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

抓取动态页面-接触爬虫

  [复制链接]
查看22120 | 回复6 | 2013-9-27 11:29:33 | 显示全部楼层 |阅读模式
楼主今天在博客园看到了这篇文章,转载过来交流下。

在ajax横行的年代,很多网页的内容都是动态加载的,而我们的小爬虫抓取的仅仅是web服务器返回给我们的html,这其中就

跳过了js加载的部分,也就是说爬虫抓取的网页是残缺的,不完整的,下面可以看下博客园首页

从首页加载中我们看到,在页面呈现后,还会有5个ajax异步请求,在默认的情况下,爬虫是抓取不到这些ajax生成的内容的,

这时候要想获取就必须调用浏览器的内核引擎来下载这些动态页面,目前内核引擎三足鼎立。

Trident: 也就是IE内核,WebBrowser就是基于该内核,但是加载性内比较差。

Gecko: FF的内核,性能相对Trident较好。

WebKit: Safari和Chrome的内核,性能你懂的,在真实场景中还是以它为主。

好了,为了简单方便,这里使用WebBrowser来玩一把,使用WebBrowser我们要注意以下几点:

第一:因为WebBrowser在System.Windows.Forms 中,属于winform控件,所以我们要设置STAThread标记。

第二:winform是事件驱动的,而Console并不会去响事件,所有事件在windows的消息队列中等待执行,为了不让程序假死,

         我们需要调用DoEvents方法转让控制权,让操作系统执行其他的事件。

第三:WebBrowser中的内容,我们需要用DomDocument来查看,而不是DocumentText。

判断一个动态网页是否加载完毕,一般常会有两种方法:

①:设定一个最大值,因为每当异步加载一个js,都会触发一个Navigating和DocumentCompleted事件,所以我们需要在此

       处记录一下count值即可。


[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace ConsoleApplication2
{
    public class Program
    {
        static int hitCount = 0;

        [STAThread]
        static void Main(string[] args)
        {
            string url = "http://www.cnblogs.com";

            WebBrowser browser = new WebBrowser();

            browser.ScriptErrorsSuppressed = true;

            browser.Navigating += (sender, e) =>
            {
                hitCount++;
            };

            browser.DocumentCompleted += (sender, e) =>
            {
                hitCount++;
            };

            browser.Navigate(url);

            while (browser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }

            while (hitCount < 16)
                Application.DoEvents();

            var htmldocument = (mshtml.HTMLDocument)browser.Document.DomDocument;

            string gethtml = htmldocument.documentElement.outerHTML;

            //写入文件
            using (StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + "//1.html"))
            {
                sw.WriteLine(gethtml);
            }

            Console.WriteLine("html 文件 已经生成!");

            Console.Read();
        }
    }
}


然后,我们打开生成好的1.html,看看js加载的内容是不是有了。②: 当然除了通过判断最大值确定是否已经加载完成,我们还可以通过设定一个Timer来判断,比如3s,4s,5s后来查看      WEBbrowser 是否加载完毕。

[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace ConsoleApplication2
{
    public class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            string url = "http://www.cnblogs.com";

            WebBrowser browser = new WebBrowser();

            browser.ScriptErrorsSuppressed = true;

            browser.Navigate(url);

            //先要等待加载完毕
            while (browser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }

            System.Timers.Timer timer = new System.Timers.Timer();

            var isComplete = false;

            timer.Elapsed += new System.Timers.ElapsedEventHandler((sender, e) =>
            {
                //加载完毕
                isComplete = true;

                timer.Stop();
            });

            timer.Interval = 1000 * 5;

            timer.Start();

            //继续等待 5s,等待js加载完
            while (!isComplete)
                Application.DoEvents();

            var htmldocument = (mshtml.HTMLDocument)browser.Document.DomDocument;

            string gethtml = htmldocument.documentElement.outerHTML;

            //写入文件
            using (StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + "//1.html"))
            {
                sw.WriteLine(gethtml);
            }

            Console.WriteLine("html 文件 已经生成!");

            Console.Read();
        }
    }
}


当然,效果依旧,就不截图了,从上面的两种写法来看,我们的WebBrowser都是放在主线程中,下面我们来看看如何放在工作线程上,很简单,只要将该工作线程设定为STA模式即可。
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ConsoleApplication2
{
    public class Program
    {
        static int hitCount = 0;

        //[STAThread]
        static void Main(string[] args)
        {
            Thread thread = new Thread(new ThreadStart(() =>
            {
                Init();
                System.Windows.Forms.Application.Run();
            }));

            //将该工作线程设定为STA模式
            thread.SetApartmentState(ApartmentState.STA);

            thread.Start();

            Console.Read();
        }

        static void Init()
        {
            string url = "http://www.cnblogs.com";

            WebBrowser browser = new WebBrowser();

            browser.ScriptErrorsSuppressed = true;

            browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);

            browser.Navigating += new WebBrowserNavigatingEventHandler(browser_Navigating);

            browser.Navigate(url);

            while (browser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }

            while (hitCount < 16)
                Application.DoEvents();

            var htmldocument = (mshtml.HTMLDocument)browser.Document.DomDocument;

            string gethtml = htmldocument.documentElement.outerHTML;

            Console.WriteLine(gethtml);
        }

        static void browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            hitCount++;
        }

        static void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            hitCount++;
        }
    }
}


C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
ibcadmin | 2013-9-28 20:33:09 | 显示全部楼层
这多好阿
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
headindotcn | 2013-9-29 15:17:40 | 显示全部楼层
谢谢分享
it招聘
daditiankong | 2013-10-15 12:11:27 | 显示全部楼层
ddd
黄东阳 | 2014-1-23 11:33:39 | 显示全部楼层
楼主 方法很给力 我本地测试都没问题  但是我传到服务器 解析不出来 我在服务器打log了 就是ajax那一段没解析出来 用这个需要注意什么吗? 求指点啊
iuugh | 2014-2-27 20:40:16 | 显示全部楼层
ddddddddddd
mars317 | 2014-6-7 22:35:32 | 显示全部楼层
先mark,以后可能会用到,谢谢分享!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则