c xpath html,C#Xpath解析HtmlDocument的使用方法与递归取得页面所有标签xpath值(附源码)...

[C#] 纯文本查看 复制代码using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Text.RegularExpressions;

using System.Threading;

using HtmlAgilityPack;

using System.IO;

using System.Runtime.Serialization.Json;

namespace AutoXpathTools

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

#region 私有变量和方法

//委托传入一个字符串

private delegate void SetListBox(string str);

//key(Xpath),value(整个节点)

List XpathList = new List();

private int Index = 0;

//htmlDcoument对象用来访问Html文档

HtmlAgilityPack.HtmlDocument hd = new HtmlAgilityPack.HtmlDocument();

#endregion

//分析Xpath的所有代码

private void btnGetXpath_Click(object sender, EventArgs e)

{

try

{

HttpHelper http = new HttpHelper();

HttpItem item = new HttpItem() { URL = textBox1.Text.Trim(), IsToLower = false, Encoding = "gbk" };

txtXml.Text = http.GetHtml(item);

if (!string.IsNullOrWhiteSpace(txtXml.Text) && txtXml.Text.Trim().ToLower() != "error")

{

//加载Html文档

hd.LoadHtml(txtXml.Text);

Thread pingTask = new Thread(new ThreadStart(delegate

{

//代码,线程要执行的代码

SartNode(txtXml.Text);

}));

pingTask.Start();

}

else

{

txtXml.Text = "根据您的的ULR:" + textBox1.Text.Trim() + "无法得到任何内容";

}

}

catch (Exception ex)

{

txtXml.Text = ex.Message.Trim();

}

}

//开始处理Node

private void SartNode(string strhtml)

{

//htmlDcoument对象用来访问Html文档s

HtmlAgilityPack.HtmlDocument hd = new HtmlAgilityPack.HtmlDocument();

//加载Html文档

hd.LoadHtml(strhtml);

HtmlNodeCollection htmllist = hd.DocumentNode.ChildNodes;

Index = 0;

XpathList.Clear();

foreach (HtmlNode em in htmllist)

{

Setxpath(em);

}

}

///

/// 递归获取Html Dom

///

/// 要处理的节点

private void Setxpath(HtmlNode node)

{

foreach (HtmlNode item in node.ChildNodes)

{

if (item.XPath.Contains("#"))

{

continue;

}

if (item.ChildNodes.Count > 0)

{

XpathList.Add(new ObjXpath() { id = Index.ToString(), Key = item.XPath, Value = "" });

UIContorol(item.XPath);

Index++;

Setxpath(item);

}

else

{

XpathList.Add(new ObjXpath() { id = Index.ToString(), Key = item.XPath, Value = "" });

UIContorol(item.XPath);

Index++;

}

}

}

//使用委托给控件赋值

private void UIContorol(string str)

{

listBox1.Items.Add(str);

toolStripStatusLabel1.Text = str;

}

private void listBox1_SelectedValueChanged(object sender, EventArgs e)

{

if (listBox1.SelectedItem != null)

{

txtPath.Text = listBox1.SelectedItem.ToString().Trim();

}

}

private void button3_Click(object sender, EventArgs e)

{

txtContents.Text = hd.DocumentNode.SelectSingleNode(txtPath.Text.Trim()).OuterHtml;

}

private void Form1_Load(object sender, EventArgs e)

{

//HttpItem item = new HttpItem()

//{

// URL = "http://www.diandian.com/login",

// Method = "post",

// Cookie = "dtid=ZfXUVo1IsplHR4mHW1HYmgKbY4GJa003; kvf=1358855337188; alf=1; dru=1356356040; _l5=y",

// ContentType = "application/x-www-form-urlencoded",

// Postdata = "account=xinsuilie1998@163.com&password=wjlove520&nextUrl=&lcallback=&persistent=1",

// Referer = "http://www.diandian.com/logout?formKey=e4714d863c862a84fafd83d98e5ecb22"

//};

//HttpHelper http = new HttpHelper();

//string html = http.GetHtml(item);

//string cookie = item.Cookie;

//item = new HttpItem() { URL = "http://www.diandian.com/home", Cookie = cookie };

//html = http.GetHtml(item);

}

}

public class ObjXpath

{

public string id { get; set; }

public string Key { get; set; }

public string Value { get; set; }

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值