C# Webscraping时System.Collections.Generic.List`1[System.String]

C# Webscraping时System.Collections.Generic.List`1[System.String],c#,linq,html-agility-pack,C#,Linq,Html Agility Pack,目前存在一个问题,我无法让C#将我的列表输出为可读的内容,这意味着我实际上无法看到webscraping是否真的起作用或提取错误信息 有人知道我如何将System.Collections.Generic.List1`[System.String]更改为可读的吗 using HtmlAgilityPack; using NScrape.Forms; using System; using System.Collections.Generic; using System.Linq; using Sy

目前存在一个问题,我无法让C#将我的列表输出为可读的内容,这意味着我实际上无法看到webscraping是否真的起作用或提取错误信息

有人知道我如何将System.Collections.Generic.List1`[System.String]更改为可读的吗

using HtmlAgilityPack;
using NScrape.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace FulcrumBotManager
{
    class Program
    {
        static void Main(string[] args)
        {

            WebClient webClient = new WebClient();
            string download = webClient.DownloadString("http://localhost:1013");

            HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
            html.LoadHtml(download);
            List<List<string>> table = html.DocumentNode.SelectSingleNode("//table")
                        .Descendants("tr")
                        .Skip(1)
                        .Where(tr => tr.Elements("td").Count() > 1)
                        .Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToList())
                        .ToList();


           table.ForEach(Console.WriteLine);
        }
    }
}
使用HtmlAgilityPack;
使用NScrape.Forms;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用系统文本;
使用System.Threading.Tasks;
命名空间管理器
{
班级计划
{
静态void Main(字符串[]参数)
{
WebClient WebClient=新的WebClient();
string download=webClient.DownloadString(“http://localhost:1013");
HtmlAgilityPack.HtmlDocument html=新的HtmlAgilityPack.HtmlDocument();
html.LoadHtml(下载);
List table=html.DocumentNode.SelectSingleNode(//table)
.后代(“tr”)
.Skip(1)
其中(tr=>tr.Elements(“td”).Count()>1)
.Select(tr=>tr.Elements(“td”).Select(td=>td.InnerText.Trim()).ToList())
.ToList();
table.ForEach(控制台写入线);
}
}
}
正在刮取的HTML

#账户{字体系列:“投石机MS”,Arial,Helvetica,无衬线;边框折叠:折叠;边距:0px自动;}账户td,客户th{字体大小:1em;边框:1px实心#98bf21;填充:3px 7px 2px 7px;}账户th{字体大小:1.1em;文本对齐:左侧;填充顶部:5px;填充底部:4px;背景色:7c942;颜色:ffffff;}#帐户tr.alt td{color:#000000;背景色:#EAF2D3;}
跑
区域
用户名
最大IP
游戏
拼写1
咒语2
召唤者
Lvl
总IP
总RP
地位
假的
欧
圣人
0
阿拉姆
障碍
治愈
召唤者
0
0
0
10:26:45断开连接
假的
欧
狡猾的
0
阿拉姆
障碍
治愈
召唤者
0
0
0
10:26:45断开连接
假的
欧
英里
0
阿拉姆
障碍
治愈
召唤者
0
0
0
10:26:46断开连接
假的
欧
曲奇
0
阿拉姆
障碍
治愈
召唤者
0
0
0
10:26:47断开连接
假的
欧
光线迷宫
0
阿拉姆
障碍
治愈
召唤者
0
0
0
10:26:48断开连接
更新的核心文件| 00:50:56:C0:00:01 00:50:56:C0:00:08 00:21:CC:73:5B:BF 08:11:96:F7:A7:0C 00:FF:8B:11:85:F4
每5秒刷新一次
基本上,您拥有的是一个
字符串列表(
:-)。这意味着它是“两个层次”

在当前状态下,您只是枚举和写入每个内部列表本身。因为
Console.WriteLine
不熟悉
列表
,所以它只调用实例上的
ToString()
,从而输出类型名

实际上,您还需要枚举内部列表:

//enumerate all lists in the outer list
foreach ( var list in table )
{
   //enumerate the inner list
   foreach ( var item in list )
   {
        //output the actual item
        Console.WriteLine( item );
   }
}

Martin的解释是正确的,只是补充了一点:你可以用LINQ这样做。

你有一个
列表
-你想如何显示它?(请不要将C#代码当作Javascript可运行的代码片段发布…@JonSkeet很抱歉以Javascript发布,我无法让其他代码真正突出显示我想要的内容。我只想看看它在拉什么,它应该从一个表中拉5行,我想看看每个表都有什么,这样我就可以格式化它了。所以用
foreach
循环遍历每个外部列表,打印一些内容,说“这是下一行”,然后以任何方式打印内部列表。现在还不清楚是什么阻碍了你。好的,谢谢,这很有效!甚至都没有想到内部列表部分!只是想通过两个嵌套的
foreach
:-)使层次结构更清晰。
table.ForEach(x => x.ForEach(Console.WriteLine));