C# 为表数据解析格式错误的HTML
我正在编写一个c#控制台应用程序,从外部html网页检索表信息 (chessnuts.org) 我想提取C# 为表数据解析格式错误的HTML,c#,regex,parsing,C#,Regex,Parsing,我正在编写一个c#控制台应用程序,从外部html网页检索表信息 (chessnuts.org) 我想提取数据,匹配,对手,结果等的所有记录-上面示例链接中的23行 我无法控制这个网页,不幸的是它的格式不好,所以我尝试过的HtmlAgilityPack和XML解析等选项都失败了。我也尝试过RegEx的一个数字,但我对这方面的知识非常贫乏,我在下面尝试了一个例子: string[] trs = Regex.Matches(html, @"&
数据
,匹配
,对手
,结果
等的所有记录-上面示例链接中的23行
我无法控制这个网页,不幸的是它的格式不好,所以我尝试过的HtmlAgilityPack
和XML
解析等选项都失败了。我也尝试过RegEx的一个数字,但我对这方面的知识非常贫乏,我在下面尝试了一个例子:
string[] trs = Regex.Matches(html,
@"<tr[^>]*>(?<content>.*)</tr>",
RegexOptions.Multiline)
.Cast<Match>()
.Select(t => t.Groups["content"].Value)
.ToArray();
我认为您只需要修复您的HtmlAgilityPack
尝试。这对我来说很好:
// Skip the first table on that page so we just get results
foreach (var table in doc.DocumentNode.SelectNodes("//table").Skip(1).Take(1)) {
foreach (var td in table.SelectNodes("//td")) {
Console.WriteLine(td.InnerText);
}
}
这会将结果表中的一堆数据(每行一列)转储到控制台。如果需要完整的程序:)。我找了好几个小时
类ReadHTML
{
internal void ReadText()
{
尝试
{
FolderBrowserDialog fbd=新建FolderBrowserDialog();
fbd.RootFolder=Environment.SpecialFolder.MyComputer;//这会导致文件夹从根文件夹或文档开始
如果(fbd.ShowDialog()==DialogResult.OK)
{
string[]files=Directory.GetFiles(fbd.SelectedPath,“*.html”,SearchOption.AllDirectories);//将此更改为指定文件类型
SaveFileDialog sfd=新建SaveFileDialog();//创建并保存CSV
//sfd.Filter=“Text File |*.txt”//仅对文本文件进行筛选
sfd.FileName=“Html Output.txt”;
sfd.Title=“保存文本文件”;
if(sfd.ShowDialog()==DialogResult.OK)
{
字符串路径=sfd.FileName;
使用(StreamWriter bw=newstreamwriter(File.Create(path)))
{
foreach(文件中的字符串f)
{
var html=new HtmlAgilityPack.HtmlDocument();
html.Load(f);
foreach(html.DocumentNode.SelectNodes(//table)中的var table)。Skip(1)。Take(1))//指定要查找的标记
{
foreach(表中的var td.SelectNodes(“//td”)//这是子标记
{
WriteLine(td.InnerText);//这将对HTML文件中要查找的内容进行文本填充
}
}
}//结束文件循环
bw.Flush();
bw.Close();
}
}
Show(“找到的文件:+Files.Count().ToString());
}
}
捕获(未经授权的访问例外UAEx)
{
MessageBox.Show(UAEx.Message);
}
捕获(PathTooLongException-PathEx)
{
MessageBox.Show(PathEx.Message);
}
}//方法结束
}
试着看一看,由于上下文无关语法解析失败的文本字符串不能是正则表达式,这难道不是重言式吗?我不知道为什么不能使用HtmlAgilityPack。。它应该能够很好地提取您在该页面上想要的内容。让我检查一下……从您的示例页面查看您的html代码,我发现您的元标记没有任何错误,这会使html Agility Pack无法解析您的html。即使您的HTML格式不正确,您也可以使用类似的方法使其美观,然后使用HTML agility packI查看页面,元标记看起来像静态字符串。用一个零长度的字符串替换它们&然后你就可以使用Html Agility PackThank了!正是我所需要的:-)没问题。很高兴我能帮忙!:)
// Skip the first table on that page so we just get results
foreach (var table in doc.DocumentNode.SelectNodes("//table").Skip(1).Take(1)) {
foreach (var td in table.SelectNodes("//td")) {
Console.WriteLine(td.InnerText);
}
}
internal void ReadText()
{
try
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.RootFolder = Environment.SpecialFolder.MyComputer;//This causes the folder to begin at the root folder or your documents
if (fbd.ShowDialog() == DialogResult.OK)
{
string[] files = Directory.GetFiles(fbd.SelectedPath, "*.html", SearchOption.AllDirectories);//change this to specify file type
SaveFileDialog sfd = new SaveFileDialog();// Create save the CSV
//sfd.Filter = "Text File|*.txt";// filters for text files only
sfd.FileName = "Html Output.txt";
sfd.Title = "Save Text File";
if (sfd.ShowDialog() == DialogResult.OK)
{
string path = sfd.FileName;
using (StreamWriter bw = new StreamWriter(File.Create(path)))
{
foreach (string f in files)
{
var html = new HtmlAgilityPack.HtmlDocument();
html.Load(f);
foreach (var table in html.DocumentNode.SelectNodes("//table").Skip(1).Take(1))//specify which tag your looking for
{
foreach (var td in table.SelectNodes("//td"))// this is the sub tag
{
bw.WriteLine(td.InnerText);// this will make a text fill of what you are looking for in the HTML files
}
}
}//ends loop of files
bw.Flush();
bw.Close();
}
}
MessageBox.Show("Files found: " + files.Count<string>().ToString());
}
}
catch (UnauthorizedAccessException UAEx)
{
MessageBox.Show(UAEx.Message);
}
catch (PathTooLongException PathEx)
{
MessageBox.Show(PathEx.Message);
}
}//method ends
}