使用HtmlAgilityPack XPath 表達(dá)式抓取博客園數(shù)據(jù)的實(shí)現(xiàn)代碼
更新時(shí)間:2011年12月01日 23:32:07 作者:
使用HtmlAgilityPack XPath表達(dá)式來(lái)抓取博客園數(shù)據(jù)使用WebClient 下載數(shù)據(jù),HtmlAgilityPack XPath表達(dá)式解析數(shù)據(jù),并綁定到Repeater控件


Web 前端代碼
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="1" cellspacing="1" bgcolor="#f1f1f1" style="text-align: center">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<tr>
<td>
標(biāo)題
</td>
<td>
發(fā)布作者
</td>
<td>
發(fā)布時(shí)間
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr bgcolor="#ffffff">
<td align="left">
<a href='<%#Eval("url") %>' target="_blank">
<%#Eval("title") %>
</a>
</td>
<td>
<a href='<%#Eval("authorUrl") %>' target="_blank">
<%#Eval("author") %>
</a>
</td>
<td>
<%#Eval("updatetime") %>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
</form>
</body>
</html>
cs 后臺(tái)代碼:
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using S1;
using System.Net;
using System.IO;
using System.Text;
using HtmlAgilityPack;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string page = string.Empty;
if (!IsPostBack)
{
WebClient wc = new WebClient();
string address = "http://www.cnblogs.com";
if (!string.IsNullOrEmpty(Request.QueryString["p"]))
{
address += "/" + Request.QueryString["p"];//分頁(yè),p=p2,p=p3
}
Stream stream = wc.OpenRead(address);
StreamReader sr = new StreamReader(stream, Encoding.UTF8);
string html = sr.ReadToEnd();
//實(shí)例化HtmlAgilityPack.HtmlDocument對(duì)象
HtmlDocument doc = new HtmlDocument();
//載入HTML
doc.LoadHtml(html);
//根據(jù)HTML節(jié)點(diǎn)NODE的ID獲取節(jié)點(diǎn)
HtmlNode navNode = doc.GetElementbyId("post_list");
//div[2]表示文章鏈接a位于post_list里面第3個(gè)div節(jié)點(diǎn)中
HtmlNodeCollection list = navNode.SelectNodes("http://div[2]/h3/a"); //根據(jù)XPATH來(lái)索引節(jié)點(diǎn)
Cnblogs cnblogs = null;
IList<Cnblogs> cnlist = new List<Cnblogs>();
foreach (HtmlNode node in list)
{
cnblogs = new Cnblogs();
//獲取文章鏈接地址
cnblogs.url = node.Attributes["href"].Value.ToString();
//獲取文章標(biāo)題
cnblogs.title = node.InnerText;
cnlist.Add(cnblogs);
}
HtmlNodeCollection list1 = navNode.SelectNodes("http://div[2]/div/a");
for (int i = 0; i < cnlist.Count; i++)
{
cnlist[i].author = list1[i].InnerText;
cnlist[i].authorUrl = list1[i].Attributes["href"].Value.ToString();
cnlist[i].updatetime = list1[i].NextSibling.InnerText.Replace("發(fā)布于", "").Trim();
}
this.Repeater1.DataSource = cnlist;
this.Repeater1.DataBind();
}
}
public class Cnblogs
{
public string title { get; set; }
public string url { get; set; }
public string author { get; set; }
public string authorUrl { get; set; }
public string updatetime { get; set; }
}
}
您可能感興趣的文章:
相關(guān)文章
ASP.NET Web API如何將注釋自動(dòng)生成幫助文檔
本篇文章主要介紹了ASP.NET Web API從注釋自動(dòng)生成幫助文檔的方法,功能很是強(qiáng)大,現(xiàn)與大家分享一下,需要的朋友可以參考下2015-07-07
asp.net實(shí)現(xiàn)三層架構(gòu)的例子
這篇文章主要介紹了asp.net實(shí)現(xiàn)三層架構(gòu)的例子,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-07-07
把ASP.NET MVC項(xiàng)目部署到本地IIS上的完整步驟
最近會(huì)經(jīng)常修改一些網(wǎng)站前端的內(nèi)容,為了方便跟UI和產(chǎn)品交流,需要將自己修改過(guò)的頁(yè)面及時(shí)發(fā)布到測(cè)試機(jī)或者是本地的IIS上。下面這篇文章主要給大家介紹了關(guān)于如何把ASP.NET MVC項(xiàng)目部署到本地IIS上的相關(guān)資料,需要的朋友可以參考下2018-06-06
ASP.NET實(shí)現(xiàn)圖形驗(yàn)證碼功能
ASP.NET 實(shí)現(xiàn)圖形驗(yàn)證碼能夠增強(qiáng)網(wǎng)站安全性,防止機(jī)器人攻擊,本文介紹了如何使用 C# 和 ASP.NET 創(chuàng)建一個(gè)簡(jiǎn)單而有效的圖形驗(yàn)證碼系統(tǒng),包括生成隨機(jī)驗(yàn)證碼、繪制驗(yàn)證碼圖像以及將圖像輸出到客戶端等步驟,感興趣的朋友一起看看吧2024-03-03
.NET更新Xml中CDATA內(nèi)容的方法實(shí)例
這篇文章介紹了.NET更新Xml中CDATA內(nèi)容的方法實(shí)例,有需要的朋友可以參考一下2013-07-07
Visual Studio 2017 15.5 正式發(fā)布!性能再提升
Visual Studio 2017 15.5 正式發(fā)布!性能再提升,時(shí)發(fā)布的還有 Visual Studio for Mac 7.3,亮點(diǎn)如下2017-12-12
自定義 DataList 顯示數(shù)據(jù)行數(shù)的方法
自定義 DataList 顯示數(shù)據(jù)行數(shù)的方法,需要的朋友可以參考一下2013-03-03
asp.net core實(shí)現(xiàn)在線生成多個(gè)文件將多個(gè)文件打包為zip返回的操作
遇到安卓手機(jī)解壓縮文件損壞問(wèn)題時(shí),可以考慮兩種解決方案,方案一是使用SharpCompress庫(kù),它是一個(gè)開(kāi)源項(xiàng)目,能夠提供強(qiáng)大的壓縮與解壓功能,支持多種文件格式,方案二是采用aspose.zip庫(kù),這兩種方法都能有效解決文件損壞的問(wèn)題2024-11-11

