佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 使用HtmlAgilityPack XPath 表達式抓取博客園數(shù)據(jù)的實現(xiàn)代碼

使用HtmlAgilityPack XPath 表達式抓取博客園數(shù)據(jù)的實現(xiàn)代碼

熱門標簽:銀行業(yè)務(wù) Linux服務(wù)器 Mysql連接數(shù)設(shè)置 阿里云 電子圍欄 服務(wù)器配置 團購網(wǎng)站 科大訊飛語音識別系統(tǒng)


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>
標題
/td>
td>
發(fā)布作者
/td>
td>
發(fā)布時間
/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();
//實例化HtmlAgilityPack.HtmlDocument對象
HtmlDocument doc = new HtmlDocument();
//載入HTML
doc.LoadHtml(html);
//根據(jù)HTML節(jié)點NODE的ID獲取節(jié)點
HtmlNode navNode = doc.GetElementbyId("post_list");
//div[2]表示文章鏈接a位于post_list里面第3個div節(jié)點中
HtmlNodeCollection list = navNode.SelectNodes("http://div[2]/h3/a"); //根據(jù)XPATH來索引節(jié)點
Cnblogs cnblogs = null;
IListCnblogs> cnlist = new ListCnblogs>();
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("發(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; }
}
}

您可能感興趣的文章:
  • c#通過xpath讀取xml示例
  • java使用xpath解析xml示例分享
  • java使用xpath和dom4j解析xml
  • DOM XPATH獲取img src值的query
  • 深入XPath的詳解以及Java示例代碼分析
  • 解析XPath語法之在C#中使用XPath的示例詳解
  • php XPath對XML文件查找及修改實現(xiàn)代碼
  • xpath的數(shù)據(jù)和節(jié)點類型以及XPath中節(jié)點匹配的基本方法
  • xpath簡介_動力節(jié)點Java學院整理

標簽:廣元 衡水 衢州 江蘇 棗莊 大理 萍鄉(xiāng) 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標題《使用HtmlAgilityPack XPath 表達式抓取博客園數(shù)據(jù)的實現(xiàn)代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    澄江县| 乐都县| 英德市| 广河县| 霍州市| 宁晋县| 吐鲁番市| 绵阳市| 外汇| 黑水县| 宁阳县| 丰台区| 儋州市| 郴州市| 长顺县| 瑞安市| 兰州市| 宜宾市| 安康市| 理塘县| 通海县| 家居| 澄城县| 佳木斯市| 合川市| 长兴县| 延津县| 温泉县| 方山县| 雷山县| 桃园县| 徐州市| 桐柏县| 海淀区| 佛冈县| 松滋市| 巴东县| 富民县| 健康| 武鸣县| 长顺县|