您的位置:首页 > 其它

超级简单的抓取网页中动态内容的方法

2014-02-23 00:00 330 查看
摘要: 超级简单的抓取网页中动态内容的方法

有时候,我们想要获取网页中的动态内容,也就是网页后台数据库中的内容,但是我们又没有权限直接访问数据库。我们只能用程序去抓取网页中的动态内容。一般网页中的动态内容都有一定的规律性。有相似的id名字前缀,有相同或者相似的className,有相似的父元素,有相似的子元素。只有有规律性,我们就能获得到它们的内容。

一、找到要抓取网页的HTML标签的规律性。

二、将规律写成算法。

三、现在任何成熟的编程语言都有一个浏览器控件,用浏览器控件把那些算法实现。

下面以糗事百科的网页内容为例。

前台代码:
<div class="block untagged mb15 bs2" id='qiushi_tag_61939100'>
<div class="author">
<img src="http://pic.qiushibaike.com/system/avtnew/980/9802917/thumb/20140223154951.jpg" alt="卖美瞳的小姑娘" />
<a href="/users/9802917" >卖美瞳的小姑娘 </a>
</div>

<div class="content" title="2014-02-23 15:01:30">

我是女汉子 体重说明一切 点多少赞我就减多少斤

</div>

<div class="thumb">

<a href="/article/61939100?list=8hr&s=4643822" target="_blank" onclick="_hmt.push(['_trackEvent', 'post', 'click', 'signlePost'])">
<img src="http://pic.qiushibaike.com/system/pictures/6193/61939100/medium/app61939100.jpg" alt="点多少赞我就减多少斤" />
</a>

</div>

<div id="qiushi_counts_61939100" class="bar">
<ul>
<li id="vote-up-61939100" class="up">
<a href="javascript:vote2(61939100,1)" id="up-61939100" title="8141个顶">8141</a>
</li>
<li id="vote-dn-61939100" class="down">
<a href="javascript:vote2(61939100,-1)" id="dn-61939100" title="-345个拍">-345</a>
</li>

<li class="comment">
<a href="/article/61939100?list=8hr&s=4643822" id="c-61939100" class="qiushi_comments" title="297条评论" target="_blank" onclick="_hmt.push(['_trackEvent', 'post', 'click', 'signlePost'])">297</a>
</li>

<li class="share">
<a href="javascript:void(0);">分享</a>
</li>
</ul>
<div class="sharebox">
<div id="bdshare" class="bdshare_t bds_tools get-codes-bdshare" data="">
<a class="bds_renren">人人网</a>
<a class="bds_qzone">QQ空间</a>
<a class="bds_tsina">新浪微博</a>
<a class="bds_tqq">腾讯微博</a>
<!---<a class="bds_mshare">一键分享</a>-->
<div class="arrow"></div>
</div>
</div>
</div>
</div>

由HTML代码可以发现,每一条糗事百科的记录均是className为'block untagged mb15 bs2'的,id前缀为'qiushi_tag_' 的DIV。

这个DIV中ClassName为author的是作者的相关信息。DIV为content的是糗事百科记录的文本信息。DIV为thumb的是糗事百科记录的图片信息。DIV为qiushi_counts是分享个评论信息。

后台代码:

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;
using System.Net;

namespace SaveWebPageToLocal
{
public partial class MainForm : Form
{
/// <summary>
///
/// </summary>
public MainForm()
{
InitializeComponent();
init_page();
}
/// <summary>
/// 初始化页面
/// </summary>
private void init_page()
{
this.WebBrower.Navigate("http://www.qiushibaike.com");
}
/// <summary>
/// 加载完成事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WebBrower_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection heList=this.WebBrower.Document.GetElementsByTagName("div");
foreach (HtmlElement currHe in heList)
{
if (currHe.GetAttribute("id").ToLower().Contains("qiushi_tag_"))
{
HtmlElementCollection heContentList=currHe.GetElementsByTagName("div");
string strId = "";
string strContrntText = "";
string strImgUrl = "";
foreach (HtmlElement currContent in heContentList)
{
if (currContent.GetAttribute("classname") == "content")
{
strContrntText = currContent.InnerText;
//获得糗事百科记录的文本信息
}

if (currContent.GetAttribute("classname") == "thumb")
{
strImgUrl = currContent.GetElementsByTagName("img")[0].GetAttribute("href");
if (strId != "")
{

(new WebClient()).DownloadFile(strImgUrl,"Images/"+(new Random()).Next(10000)+".jpg");
//获得糗事百科记录的图片链接地址,并插入到本地
}
}

}
}
}
MessageBox.Show("OK");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息