用DOM实现文章采集--通过jquery语法式的方法采集指定对象的文本
[csharp]
/// <summary>
/// DOM查询器,用法跟jquery差不多
/// </summary>
public class DomQuery
{
/// <summary>
/// 获得节点
/// </summary>
/// <param name="_HtmlDocument"></param>
/// <param name="selector"></param>
/// <returns></returns>
/// <remarks>DOM选择器,用法跟jquery差不多</remarks>
public IList<HtmlNode> Get(HtmlDocument _HtmlDocument, string selector)
{
string[] Expressions = selector.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
List<HtmlNode> hnList = new List<HtmlNode>();
if (Expressions[0].StartsWith("#"))
{
hnList.Add(_HtmlDocument.GetElementbyId(Expressions[0].TrimStart('#')));
hnList.RemoveAll(x => { return x == null; });
if (Expressions.Length == 1)
{
return hnList;
}
for (int i = 1; i < Expressions.Length; i++)
{
hnList = Get(hnList, Expressions[i]);
}
}
else
{
hnList.AddRange(_HtmlDocument.DocumentNode.ChildNodes.Where(x => { return x.NodeType == HtmlNodeType.Element; }));
for (int i = 0; i < Expressions.Length; i++)
{
hnList = Get(hnList, Expressions[i]);
相关新闻>>
- 发表评论
-
- 最新评论 更多>>