您获得此 javascript 的站点似乎不希望您解析其 html。它使用 javascript 函数创建动态 html wr
。下面是执行此 javascript 并解析结果代码的代码。但是我不能说这是一个简单的跟踪代码
public void Test()
{
//C# object which will be accessed by javascript
var csharpObj = new MyCSharpObject();
//Create Javascript object
Type scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));
dynamic obj = Activator.CreateInstance(scriptType, false);
obj.Language = "Javascript";
obj.AddObject("csharp", csharpObj);
//Load Html (your string in question)
string html = @"<script type=""text/javascript"">wr(""<span>maddog"");wr(""@"");wr(""website-url.com</span>"")</script>";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
//Create "wr" function
string script = "function wr(s){csharp.wr(s);}";
//Get the text of script tag
script += doc.DocumentNode.SelectSingleNode("//script").InnerText;
//Execute script
obj.Eval(script);
//Load the string created by javascript execution
doc.LoadHtml(csharpObj.Output);
//tada.....
var eMailAddress = doc.DocumentNode.InnerText;
Console.WriteLine(eMailAddress);
}
[ComVisible(true)]
public class MyCSharpObject
{
public string Output = "";
public void wr(string s)
{
Output += s;
}
}
- - - - 编辑 - - - -
我不确定如何编写“获取所有 wr(*) 字符串
尽管您似乎想要这样的解决方案,但我不会依赖正则表达式来解析 html
public void Test2()
{
string html = @"<script type=""text/javascript"">wr(""<span>maddog"");wr(""@"");wr(""website-url.com</span>"")</script>";
var parsedHtml = String.Join("",Regex.Matches(html, @"wr\(\""(.+?)\""\)")
.Cast<Match>()
.Select(m => m.Groups[1].Value));
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(parsedHtml);
var eMailAddress = doc.DocumentNode.InnerText;
}