C#分类中的HTML解析
我正在进行情感分类,我正在解析来自本地电影数据库的数据。问题是它们有三种分类形式。一个有明星(在…)一个“垃圾”,没有给明星或称之为垃圾,这里是它的主要链接:你需要检查源代码-这里是一个例子,你可以看到所有三种类型的电影用户评价C#分类中的HTML解析,c#,html,parsing,classification,C#,Html,Parsing,Classification,我正在进行情感分类,我正在解析来自本地电影数据库的数据。问题是它们有三种分类形式。一个有明星(在…)一个“垃圾”,没有给明星或称之为垃圾,这里是它的主要链接:你需要检查源代码-这里是一个例子,你可以看到所有三种类型的电影用户评价 </li> <li id="comment-8356897"> <h5 class="author"><a href="/uzivatel/138463-campbell/">Campbell</a>&
</li>
<li id="comment-8356897">
<h5 class="author"><a href="/uzivatel/138463-campbell/">Campbell</a></h5>
<img src="http://img.csfd.cz/assets/images/rating/stars/2.gif" class="rating" width="16" alt="**" />
<div class="info">
<a href="/uzivatel/138463-campbell/komentare/">všechny komentáře uživatele</a></div>
<p class="post">Ale jo:-D Když jsem viděl že tenhle film je na prvním místě mezi největšíma sračkama na CSFD, a tak jsem se zhrozil a abych si utrpení ještě vylepšil, tak jsem si pustil oba dva díly naráz. No hell to celkem bylo ale ne nic extrémní. Viděl jsem větší shity. V tomhle filmu jsem měl děsnej problém fandit někomu fandit protože to moc nejde. Šílenost, Ale ne nejhorší.<span class="date desc">(11.3.2011)</span></p>
</li>
<li id="comment-872277">
<h5 class="author"><a href="/uzivatel/48974-fleker/">fleker</a></h5>
<div class="info">
<a href="/uzivatel/48974-fleker/komentare/">všechny komentáře uživatele</a></div>
<p class="post">tak na todle rači ani koukat nebudu; hodnocení to má slušný ale nechci riskovat aby mi vyschla mícha<span class="date desc">(29.7.2009)</span></p>
</li>
<li id="comment-327360">
<h5 class="author"><a href="/uzivatel/41698-ozo/">Ozo</a></h5>
<strong class="rating">odpad!</strong>
<div class="info">
<a href="/uzivatel/41698-ozo/komentare/">všechny komentáře uživatele</a></div>
<p class="post">Změna názoru - tohle si jednu hvězdičku nezaslouží =(<span class="date desc">(29.7.2007)</span></p>
</li>
这段代码的问题是,如果找不到att.InnerText==“odpad!”或att.Attributes[“alt”]!=null它继续到下一篇文章,并从那里获取用户评估。但我想至少将一些内容与评估发布的帖子进行匹配。“odpad!”不在属性中,而是在元素中。如果更改
if
语句会怎么样。如果只有一个if
语句是真的,为什么还要有3个if
语句
// Is it "odpad" ?
if (att.InnerText == "odpad!")
{
b[j] = att.InnerText;
}
// .. If not, is it starred?
else if (att.Attributes["alt"] != null)
{
b[j] = att.Attributes["alt"].Value;
}
// If none of above, it must be this (default)
else
{
b[j] = "without user evaluation";
}
感谢所有帮助,但问题出在html的xpath中 我是这样解决的
string srxPathOfCategory = "//ul[@class='ui-posts-list']//li";
foreach (var att in doc.DocumentNode.SelectNodes(srxPathOfCategory))
{
foreach (var child in att.ChildNodes.Skip(3)) // skipping first three nodes //- first one is whitespace - marked as #text child node, then there is h5 and third is //another whitespace marked as #text child node
{
if (child.InnerText == "odpad!")
{
b[j] = child.InnerText;
Console.WriteLine(b[j]);
Console.ReadKey();
break;
}
else if (child.Attributes["alt"] != null)
{
b[j] = child.Attributes["alt"].Value;
Console.WriteLine(b[j]);
Console.ReadKey();
break;
}
else
{
b[j] = "without user evaluation";
Console.WriteLine("hlupost");
Console.ReadKey();
break;
}
}
j++;
}
我不知道,但那对我没有帮助。该程序可以解析“odpad”,也可以解析从1*到5****。问题是,当它在没有像“odpad”这样的评估的情况下找到post时或者5****它跳转到下一篇文章,从那里进行评估,因此混合了正确的帖子和评估顺序。如果您将
if
语句替换为if-else
语句,那么您的最后一个else可能是默认评估,这将处理odpad和stars,但不会处理任何评估,因此,看起来属性[“alt”]总是不为null
string srxPathOfCategory = "//ul[@class='ui-posts-list']//li";
foreach (var att in doc.DocumentNode.SelectNodes(srxPathOfCategory))
{
foreach (var child in att.ChildNodes.Skip(3)) // skipping first three nodes //- first one is whitespace - marked as #text child node, then there is h5 and third is //another whitespace marked as #text child node
{
if (child.InnerText == "odpad!")
{
b[j] = child.InnerText;
Console.WriteLine(b[j]);
Console.ReadKey();
break;
}
else if (child.Attributes["alt"] != null)
{
b[j] = child.Attributes["alt"].Value;
Console.WriteLine(b[j]);
Console.ReadKey();
break;
}
else
{
b[j] = "without user evaluation";
Console.WriteLine("hlupost");
Console.ReadKey();
break;
}
}
j++;
}