国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

使用HtmlAgilityPack XPath 表達式抓取博客園數據的實現代碼

2024-07-10 13:24:27
字體:
來源:轉載
供稿:網友

使用HtmlAgilityPack XPath 表達式抓取博客園數據的實現代碼


使用HtmlAgilityPack XPath 表達式抓取博客園數據的實現代碼

Web 前端代碼

復制代碼 代碼如下:


<%@ 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 runat="server">
<div>
<table cellpadding="1" cellspacing="1" bgcolor="#f1f1f1">
<asp:Repeater runat="server">
<HeaderTemplate>
<tr>
<td>
標題
</td>
<td>
發布作者
</td>
<td>
發布時間
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr bgcolor="#ffffff">
<td>
<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 后臺代碼:

復制代碼 代碼如下:


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 += "http://m.survivalescaperooms.com/" + Request.QueryString["p"];//分頁,p=p2,p=p3
}
Stream stream = wc.OpenRead(address);
StreamReader sr = new StreamReader(stream, Encoding.UTF8);
string html = sr.ReadToEnd();
//實例化HtmlAgilityPack.HtmlDocument對象
HtmlDocument doc = new HtmlDocument();
//載入HTML
doc.LoadHtml(html);
//根據HTML節點NODE的ID獲取節點
HtmlNode navNode = doc.GetElementbyId("post_list");
//div[2]表示文章鏈接a位于post_list里面第3個div節點中
HtmlNodeCollection list = navNode.SelectNodes("http://div[2]/h3/a"); //根據XPATH來索引節點
Cnblogs cnblogs = null;
IList<Cnblogs> cnlist = new List<Cnblogs>();
foreach (HtmlNode node in list)
{
cnblogs = new Cnblogs();
//獲取文章鏈接地址
cnblogs.url = node.Attributes["href"].Value.ToString();
//獲取文章標題
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("發布于", "").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; }
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 介休市| 西乌珠穆沁旗| 岢岚县| 庐江县| 荣昌县| 砚山县| 天长市| 麻城市| 扶绥县| 思南县| 锡林郭勒盟| 肃宁县| 朝阳县| 平顶山市| 城口县| 青阳县| 乌拉特前旗| 泰和县| 金华市| 台东县| 南岸区| 临朐县| 东乌珠穆沁旗| 肃北| 咸宁市| 景宁| 无为县| 电白县| 宾川县| 米林县| 云林县| 桦川县| 绍兴县| 鸡泽县| 开江县| 家居| 岳池县| 织金县| 马公市| 呼伦贝尔市| 尖扎县|