C# 在标记中查找javascript并确定它是否位于title标记之上的正确方法是什么?
我需要查看html页面标记是否在源代码、脚本块中包含google analytics,以及脚本块是否位于C# 在标记中查找javascript并确定它是否位于title标记之上的正确方法是什么?,c#,javascript,asp.net,C#,Javascript,Asp.net,我需要查看html页面标记是否在源代码、脚本块中包含google analytics,以及脚本块是否位于标记上方 我已经设法将网页的源代码放入一个变量中 我正在努力编写正确的正则表达式来提取google分析部分的代码,首先要知道它是否存在,其次是javascript是否在标记之前 有什么建议吗?避免使用正则表达式解析html;陷阱太多了。假设您搜索字符串“我希望,您没有尝试使用正则表达式解析html?正确的方法是什么?使用一些html解析器(例如HtmlAgilityPack)来完成此操作。请不
标记上方
我已经设法将网页的源代码放入一个变量中
我正在努力编写正确的正则表达式来提取google分析部分的代码,首先要知道它是否存在,其次是javascript是否在标记之前
有什么建议吗?避免使用正则表达式解析html;陷阱太多了。假设您搜索字符串“我希望,您没有尝试使用正则表达式解析html?正确的方法是什么?使用一些html解析器(例如HtmlAgilityPack)来完成此操作。请不要尝试使用正则表达式解析html。我第二次使用HtmlAgilityPack。我需要说更多:)
using HtmlAgilityPack;
....
HtmlDocument doc = new HtmlDocument();
doc.Load(fileName);
var titles = doc.DocumentNode.SelectNodes("/html/head/title");
if (titles != null)
{
foreach(var title in titles)
{
Console.WriteLine("<title> on line: " + title.Line);
}
var scripts = doc.DocumentNode.SelectNodes("/html/head/script");
if (scripts != null)
{
foreach(var script in scripts)
{
Console.WriteLine("<script> on line: " + script.Line);
// here, you need to decide if the script is before the title
// and if it is the "right" script - google analytics.
// you have to do that part yourself.
}
}
else
{
Console.WriteLine("No script nodes found.");
}
}
else
{
Console.WriteLine("No title node found.");
}