Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用XPath查找所有JavaScript类型脚本元素_C#_Linq_Xpath_Html Agility Pack - Fatal编程技术网

C# 使用XPath查找所有JavaScript类型脚本元素

C# 使用XPath查找所有JavaScript类型脚本元素,c#,linq,xpath,html-agility-pack,C#,Linq,Xpath,Html Agility Pack,我试图通过使用HTMLAgilityPack(加上LINQ和XPath)在一系列HTML文档中查找所有元素。 这些文档的页眉中有脚本元素,页脚中有谷歌分析。首先,我尝试以头脚本为目标并删除它们。我的记事本++告诉我,我有719个脚本元素,但我的控制台应用程序只找到其中的55个 我需要som帮助来正确定位它们,以便从文档中删除它们 源文件(head的结构) 非层次文档聚类 //Javascript代码在这里 _uacct=“UA-67XXXX-X”; 海胆追踪者(); 到目前为止,我已经尝试使

我试图通过使用HTMLAgilityPack(加上LINQ和XPath)在一系列HTML文档中查找所有
元素。 这些文档的页眉中有脚本元素,页脚中有谷歌分析。首先,我尝试以头脚本为目标并删除它们。我的记事本++告诉我,我有719个脚本元素,但我的控制台应用程序只找到其中的55个

我需要som帮助来正确定位它们,以便从文档中删除它们

源文件(head的结构)


非层次文档聚类
//Javascript代码在这里
_uacct=“UA-67XXXX-X”;
海胆追踪者();
到目前为止,我已经尝试使用JavaScript来定位“语言”类型,但在解析html/head时只得到了一些命中率。我的方法从列表中获取文件名。现在,该方法打印出列表中收集的脚本数,这将更改为“scripts.Remove();”一旦我得到正确的搜索字符串

private static void FindTagsToRemove(IEnumerable<string> files)
{
    var doc = new HtmlDocument();
    List<string> scripts = new List<string>();
    List<string> errors = new List<string>();
    try
    {
        foreach (var file in files)
        {
            doc.Load(@file);
            var head = doc.DocumentNode.SelectSingleNode("html/head");
            var nodes = new List<HtmlNode>();
            bool isScript = false;

            foreach (var node in head.ChildNodes.ToList())
            {
                if (node.NodeType == HtmlNodeType.Element && node.Name.Contains("script"))
                {
                    isScript = !isScript;
                    scripts.Add(node.OuterHtml);
                    Console.WriteLine(node.OuterHtml);
                }
                else if (isScript)
                {
                    nodes.Add(node);
                    node.Remove();
                }
            }
        }
        int nr_scripts = scripts.Count();
        Console.WriteLine("Number of scripts in collection: {0}", nr_scripts);
    }
    catch (Exception Ex)
    {
        Console.WriteLine(Ex.Message);
    }
}
private static void FindTagsToRemove(IEnumerable文件)
{
var doc=新的HtmlDocument();
列表脚本=新列表();
列表错误=新列表();
尝试
{
foreach(文件中的var文件)
{
doc.Load(@file);
var head=doc.DocumentNode.SelectSingleNode(“html/head”);
var节点=新列表();
bool-isScript=false;
foreach(head.ChildNodes.ToList()中的var节点)
{
if(node.NodeType==HtmlNodeType.Element&&node.Name.Contains(“脚本”))
{
isScript=!isScript;
添加(node.OuterHtml);
Console.WriteLine(node.OuterHtml);
}
else if(isScript)
{
nodes.Add(node);
node.Remove();
}
}
}
int nr_scripts=scripts.Count();
WriteLine(“集合中的脚本数:{0}”,nr_脚本);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
}
如果有人有更好的方法在head节点中定位JavaScripts,那将非常感谢。感谢您的帮助!:)

如果只需要
元素节点,请使用。示例HTML:

var html =
@"<!doctype html system 'html.dtd'>
<html>
<head>
<link rel='stylesheet' href='../IRstyle.css' type='text/css'>
<title>Non-hierarchic document clustering</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<meta name='keywords' content=''>
<meta name='VW96.objecttype' content='Document'>
<script language='JavaScript' type='text/JavaScript'>
//Javascript-code goes here
</script>
</head>
<body>    
<!--Body contents goes here-->

<!-- in footer -->
<script src='http://www.google-analytics.com/urchin.js' type='text/javascript'>
</script>
<script type='text/javascript'>
_uacct = 'UA-67XXXX-X';
urchinTracker();
</script>
</body>
</html>";
var-html=
@"
非层次文档聚类
//Javascript代码在这里
_uacct='UA-67XXXX-X';
海胆追踪者();
";
分析示例:

var document = new HtmlDocument();
document.LoadHtml(html);
// target only <script> in <head>
// var scriptTags = document.DocumentNode.SelectNodes("//head/script");
var scriptTags = document.DocumentNode.SelectNodes("//script");

foreach (var script in scriptTags) script.Remove();    

document.Save(OUTPUT);
var document=newhtmldocument();
document.LoadHtml(html);
//目标仅在
//var scriptTags=document.DocumentNode.SelectNodes(“//head/script”);
var scriptTags=document.DocumentNode.SelectNodes(“//脚本”);
foreach(scriptTags中的var脚本)script.Remove();
文件保存(输出);
输出:

<!doctype html system 'html.dtd'>
<html>
<head>
<link rel='stylesheet' href='../IRstyle.css' type='text/css'>
<title>Non-hierarchic document clustering</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<meta name='keywords' content=''>
<meta name='VW96.objecttype' content='Document'>

</head>
<body>    
<!--Body contents goes here-->

<!-- in footer -->


</body>
</html>

非层次文档聚类

谢谢!非常感谢!:)现在,我只需要找到一些正确的错误处理,使我的程序继续运行的错误。某些文件无法读取并抛出NullReference。也许还有一个问题请记住,我通过将try/catch放在foreach循环中来管理错误处理。现在我只需要找出为什么html agility不能读取html文件。
<!doctype html system 'html.dtd'>
<html>
<head>
<link rel='stylesheet' href='../IRstyle.css' type='text/css'>
<title>Non-hierarchic document clustering</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<meta name='keywords' content=''>
<meta name='VW96.objecttype' content='Document'>

</head>
<body>    
<!--Body contents goes here-->

<!-- in footer -->


</body>
</html>