=波波日志 > Asp.Net/C#/WCF > AJax网络爬虫设计与实现=
AJax网络爬虫设计与实现
AJax网络爬虫设计与实现
关键字:网络爬虫 网络蜘蛛 搜索引擎 ajax JavaScript 数据采集 webspider
传统意义上的网络爬虫是不解析JavaScript生成的内容的,所以JavaScript+ajax生成的内容对于传统的搜索引擎很不友好,不利于SEO。
本例子使用winForm编程中的WebBrowser控件来加载页面,并解析页面内容导入的JavaScript文件生成的页面内容,获取body的innerHTML,这样就不用害怕网络爬虫抓取不到ajax或者JavaScript动态生成的内容。对于要采集一些用JavaScript+ajax动态生成的网页,此例子可作为参考。不过最终要采集需要的数据,还得自己用正则表达式或者其他方法来分析采集下来的html代码,获取对自己有用的数据。
由于使用b/s方法的iframe也可以实现上面的内容加载并解析,但由于浏览器存在跨域的安全问题,获取iframe中的内容时会出现安全问题,所以采用winForm编程。
最近看到的一片文章说Google爬虫即将同时解析JavaScript或者ajax生成的内容,不知道是否真实。详细参考下面的文章。
Google蜘蛛运行网页中JS和CSS样式
下面是本示例中核心代码
示例代码下载地址:Ajax WebSplider网络爬虫
关键字:网络爬虫 网络蜘蛛 搜索引擎 ajax JavaScript 数据采集 webspider
传统意义上的网络爬虫是不解析JavaScript生成的内容的,所以JavaScript+ajax生成的内容对于传统的搜索引擎很不友好,不利于SEO。
本例子使用winForm编程中的WebBrowser控件来加载页面,并解析页面内容导入的JavaScript文件生成的页面内容,获取body的innerHTML,这样就不用害怕网络爬虫抓取不到ajax或者JavaScript动态生成的内容。对于要采集一些用JavaScript+ajax动态生成的网页,此例子可作为参考。不过最终要采集需要的数据,还得自己用正则表达式或者其他方法来分析采集下来的html代码,获取对自己有用的数据。
由于使用b/s方法的iframe也可以实现上面的内容加载并解析,但由于浏览器存在跨域的安全问题,获取iframe中的内容时会出现安全问题,所以采用winForm编程。
最近看到的一片文章说Google爬虫即将同时解析JavaScript或者ajax生成的内容,不知道是否真实。详细参考下面的文章。
Google蜘蛛运行网页中JS和CSS样式
下面是本示例中核心代码
+展开
-C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AjaxSpider
{
public partial class main : Form
{
public main()
{
InitializeComponent();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(onDocumentLoaded);
}
private void onDocumentLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
if (wb != null) txtHTML.Text = wb.Document.Body.InnerHtml;
btnDown.Enabled = true;
btnDown.Text = "下载";
}
private void btnDown_Click(object sender, EventArgs e)
{
if (txtUrl.Text == "" || (!txtUrl.Text.StartsWith("http://") && !txtUrl.Text.StartsWith("https://")))
{
MessageBox.Show("请输入正确的URL地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtUrl.Select();
}
else
{
btnDown.Text = "正在解析页面...";
btnDown.Enabled = false;
wb.Navigate(txtUrl.Text);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AjaxSpider
{
public partial class main : Form
{
public main()
{
InitializeComponent();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(onDocumentLoaded);
}
private void onDocumentLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
if (wb != null) txtHTML.Text = wb.Document.Body.InnerHtml;
btnDown.Enabled = true;
btnDown.Text = "下载";
}
private void btnDown_Click(object sender, EventArgs e)
{
if (txtUrl.Text == "" || (!txtUrl.Text.StartsWith("http://") && !txtUrl.Text.StartsWith("https://")))
{
MessageBox.Show("请输入正确的URL地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtUrl.Select();
}
else
{
btnDown.Text = "正在解析页面...";
btnDown.Enabled = false;
wb.Navigate(txtUrl.Text);
}
}
}
}
示例代码下载地址:Ajax WebSplider网络爬虫
类别:Asp.Net/C#/WCF 作者:波波 日期:2010-07-27 【评论:0】
暂时没有评论!
发表留言
百度赞助
同类热门博文
- ·IE里Cookie跨域不能..
- ·去掉隐藏asp.net编译..
- ·解决asp.net验证视图..
- ·找不到System.Web.S..
- ·web服务因URL意外地..
- ·C#2.0中,SerialPor..
- ·用C#编写ActiveX控件..
- ·用C#编写ActiveX控件..
博格Tag
- flash/flex/fcs/AIR(752)
- Asp.Net/C#/WCF(598)
- 操作系统及应用软件(376)
- JavaScript/Ajax(330)
- SQL及数据库(134)
- 黑客技术(115)
- Asp/VBScript(111)
- HTML/WML/CSS兼容/XML(102)
- PHP/apache/Perl(96)
- 网站排名及优化(96)
- 其他(75)
- showbo日志(66)
- lucene.net/分词技术(33)
- 计算机网络(26)
- 机械重工(26)
- C#设计模式(25)
- Google Maps开发(17)
- 日语学习(15)
- Canvas/VML/SVG(13)
- linux(11)
- 游戏开发(8)
- 正则表达式(5)
- Jsp/Java(4)
最新博文
- ·详解SqlConnection连..
- ·C#实现的html内容截..
- ·asp.net web.config..
- ·asp.net<%--注释--%..
- ·ASP.NET环境配置常见..
- ·asp.net防止图片盗链..
- ·Session.Abandon的使..
- ·asp中缓存cache技术..
随机博文
