使用HtmlAgilityPack XPath 表達(dá)式抓取博客園數(shù)據(jù)的實(shí)現(xiàn)代碼
更新時(shí)間:2011年12月01日 23:32:07 作者:
使用HtmlAgilityPack XPath表達(dá)式來抓取博客園數(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 后臺代碼:
復(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"];//分頁,p=p2,p=p3
}
Stream stream = wc.OpenRead(address);
StreamReader sr = new StreamReader(stream, Encoding.UTF8);
string html = sr.ReadToEnd();
//實(shí)例化HtmlAgilityPack.HtmlDocument對象
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來索引節(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?MVC前臺動(dòng)態(tài)添加文本框并在后臺使用FormCollection接收值
這篇文章介紹了ASP.NET?MVC前臺動(dòng)態(tài)添加文本框并在后臺使用FormCollection接收的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08visual Studio 2017創(chuàng)建簡單控制臺程序
這篇文章主要為大家詳細(xì)介紹了visual Studio 2017創(chuàng)建簡單控制臺程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Visual Studio尋找C#程序必要的運(yùn)行庫文件
這篇文章主要為大家詳細(xì)介紹了Visual Studio尋找C#程序必要的運(yùn)行庫文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05asp.net 獲取目錄下的文件數(shù)和文件夾數(shù)
遍歷一個(gè)文件夾中的文件,需要用到DirectoryInfo類中的一個(gè)重要的方法GetFileSystemInfos(),此方法返回指定的是與搜索條件相匹配的文件和子目錄的強(qiáng)類型 FileSystemInfo對象的數(shù)組。2010-07-07.NET?6開發(fā)TodoList應(yīng)用實(shí)現(xiàn)系列背景
這篇文章主要介紹了.NET?6開發(fā)TodoList應(yīng)用實(shí)現(xiàn)系列背景,NET?6是一個(gè)很優(yōu)秀的框架,這一點(diǎn)自從我最開始接觸.NET?Core?2起一年一年進(jìn)化到現(xiàn)在,就深切地感受到,那好東西就拿出來和大家分享一下,下面來看一下文章的學(xué)習(xí)介紹吧2021-12-12