Web Front-End Code
Copy Code code as follows:
<%@ 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 ">
<title></title>
<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>
Title
</td>
<td>
Publish author
</td>
<td>
Publish Time
</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>
CS Background Code:
Copy Code code as follows:
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"];//pagination, P=P2,P=P3
}
Stream stream = WC. OpenRead (address);
StreamReader sr = new StreamReader (stream, Encoding.UTF8);
String html = Sr. ReadToEnd ();
Instantiating a Htmlagilitypack.htmldocument object
HTMLDocument doc = new HTMLDocument ();
Load HTML
Doc. loadhtml (HTML);
Gets the node based on the ID of the HTML nodes node
Htmlnode Navnode = doc. getElementById ("Post_list");
DIV[2] means that article link A is located in the 3rd div node inside the post_list
Htmlnodecollection list = Navnode.selectnodes ("//div[2]/h3/a"); To index a node based on XPath
Cnblogs cnblogs = null;
ilist<cnblogs> cnlist = new list<cnblogs> ();
foreach (Htmlnode node in list)
{
Cnblogs = new Cnblogs ();
Get article link address
Cnblogs.url = node. attributes["href"]. Value.tostring ();
Get article title
Cnblogs.title = node. InnerText;
Cnlist. ADD (Cnblogs);
}
Htmlnodecollection List1 = navnode.selectnodes ("//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 ("Posted on", ""). 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;}
}
}