Using Htmlagilitypack XPath expressions to crawl blog Garden Data Implementation Code _ Practical Tips

Source: Internet
Author: User
Tags xpath

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:// ">
<form id= "Form1" runat= "Server" >
<table cellpadding= "1" cellspacing= "1" bgcolor= "#f1f1f1" style= "Text-align:center" >
<asp:repeater id= "Repeater1" runat= "Server" >
Publish author
Publish Time
<tr bgcolor= "#ffffff" >
&LT;TD align= "Left" >
<a href= ' <% #Eval ("url")%> ' target= ' _blank ' >
<% #Eval ("title")%>
<a href= ' <% #Eval ("Authorurl")%> ' target= ' _blank ' >
<% #Eval ("author")%>
<% #Eval ("UpdateTime")%>

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
protected void Page_Load (object sender, EventArgs e)
String page = string. Empty;
if (! IsPostBack)
WebClient WC = new WebClient ();
string address = "";
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 ();
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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.