forked from szgit/Helper.Core.Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HtmlCollectionHelper.cs
68 lines (59 loc) · 2.96 KB
/
HtmlCollectionHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* 作用:通过 HtmlAgilityPack 和 XPath 实现页面数据采集。
* 联系:QQ 100101392
* 来源:https://github.com/snipen/Helper.Core.Library
* */
using HtmlAgilityPack;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Helper.Core.Library
{
public class HtmlCollectionHelper
{
#region 对外公开方法
/// <summary>
/// 异步获取数据
/// </summary>
/// <param name="analysisEnum">解析模式</param>
/// <param name="httpItem">HttpItem</param>
/// <param name="xPath">XPath 查询语句</param>
/// <param name="xPathMatchList">属性 XPath 查询语句列表</param>
/// <param name="propertyMatchList">属性匹配,Dictionary<string, object> 或 new {}</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public async Task<List<T>> AsyncToList<T>(HtmlAnalysisEnum analysisEnum, HttpItem httpItem, string xPath, List<XPathMatch> xPathMatchList = null, object propertyMatchList = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new()
{
HttpHelper httpHelper = new HttpHelper();
HttpResult httpResult = httpHelper.GetHtml(httpItem);
if (httpResult == null) return null;
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(httpResult.Html);
List<T> dataList = null;
await Task.Run(() =>
{
dataList = HtmlAnalysisHelper.ToEntityList<T>(analysisEnum, htmlDocument, xPath, xPathMatchList, propertyMatchList, reflectionType);
});
return dataList;
}
/// <summary>
/// 同步获取数据
/// </summary>
/// <param name="analysisEnum">解析模式</param>
/// <param name="httpItem">HttpItem</param>
/// <param name="xPath">XPath 查询语句</param>
/// <param name="xPathMatchList">属性 XPath 查询语句列表</param>
/// <param name="propertyMatchList">属性匹配,Dictionary<string, object> 或 new {}</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public List<T> ToList<T>(HtmlAnalysisEnum analysisEnum, HttpItem httpItem, string xPath, List<XPathMatch> xPathMatchList = null, object propertyMatchList = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new()
{
HttpHelper httpHelper = new HttpHelper();
HttpResult httpResult = httpHelper.GetHtml(httpItem);
if (httpResult == null) return null;
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(httpResult.Html);
return HtmlAnalysisHelper.ToEntityList<T>(analysisEnum, htmlDocument, xPath, xPathMatchList, propertyMatchList, reflectionType);
}
#endregion
}
}