尝试使用AngleSharp解析wiki页面

时间:2018-04-29 02:34:23

标签: anglesharp

我正在尝试解析:List of airlines (Wikipedia)

该页面有一个简单的表格:

<table class="wikitable sortable">
    <caption>Airline codes</caption>
    <tr>
        <th>IATA</th>
        <th>ICAO</th>
        <th>Airline</th>
        <th>Call sign</th>
        <th>Country</th>
        <th>Comments</th>
    </tr>

    etc...

我知道这可以用正则表达式轻松解析,但我从未使用过AngleSharp,我想弄明白。

我做了一段简单的代码:

var parser = new HtmlParser();
var config = new Configuration();

var document = BrowsingContext.New(config).OpenAsync(Url.Create("https://en.wikipedia.org/wiki/List_of_airline_codes")).Result;

var aa = document.QuerySelectorAll("tr");
var bb = document.QuerySelectorAll("wikitable");
var cc = document.QuerySelectorAll("table");

页面正确加载,但我的查询都没有返回任何内容。我错过了什么?

1 个答案:

答案 0 :(得分:1)

默认Configuration不支持文档加载,因此您获得一个空文档。使用WithDefaultLoader加载配置。

所以改变

var config = new Configuration();

var config = Configuration.Default.WithDefaultLoader();