C#中的GetElementsByClassName方法
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public static class Spread
{
/// <summary>
/// 检索指定class的所有HtmlDocument对象
/// </summary>
/// <param name="doc"></param>
/// <param name="className"></param>
/// <returns></returns>
public static HtmlElement GetElementsByClassName(this HtmlDocument doc, string className)
{
HtmlElementCollection collection = doc.All;
//HtmlElementCollection collection = doc.Body.All;
HtmlElement html = doc.CreateElement("");
foreach (HtmlElement he in collection)
{
if (he.GetAttribute("classname") == className)
{
html.AppendChild(he);
}
}
return html;
|