-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFormParser.cs
More file actions
48 lines (43 loc) · 1.52 KB
/
FormParser.cs
File metadata and controls
48 lines (43 loc) · 1.52 KB
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
using System;
using HtmlAgilityPack;
using ScrapySharp.Extensions;
using Smallcode.Net.Extensions;
namespace Smallcode.Net
{
public class FormParser
{
private readonly HtmlNode _form;
public FormParser(string html)
{
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
_form = htmlDocument.DocumentNode.SelectSingleNode("//form[1]");
if (_form == null) throw new Exception("ÔÚHTMLÔ´´úÂëÖÐÕÒ²»µ½Form±êÇ©");
}
public FormData FormData
{
get
{
var data = new FormData();
var nodes = _form.SelectNodes("//input[@type!='submit'] | //input[@type!='button'] | //select | //textarea");
if (nodes == null) return data;
foreach (var node in nodes)
{
var name = node.GetAttributeValue("name");
if (!(string.IsNullOrWhiteSpace(name) || data.ContainsKey(name)))
data.Set(name, node.GetAttributeValue("value", ""));
}
return data;
}
}
public string GetAction(Uri baseUri)
{
return _form.GetAttributeValue("action", "").MakeAbsoluteUri(baseUri);
}
public string GetImageUrl(Uri baseUri, string xPath)
{
var img = _form.SelectSingleNode(xPath);
return img != null ? img.GetAttributeValue("src").MakeAbsoluteUri(baseUri) : string.Empty;
}
}
}