私はHTMLの解析のためのHTMLのアジリティパックとので、私はいくつかの悪いものに遭遇:Grateのもの:|これは私の背景コードです
public static HtmlDocument GetXHtmlFromUri2(string uri)
{
HttpClient client = HttpClientFactory.Create(new CustomeHeaderHandler());
var htmlDoc = new HtmlDocument()
{
OptionCheckSyntax = true,
OptionFixNestedTags = true,
OptionAutoCloseOnEnd = true,
OptionReadEncoding = true,
OptionDefaultStreamEncoding = Encoding.UTF8,
};
htmlDoc.LoadHtml(client.GetStringAsync(uri).Result);
return htmlDoc;
}
私はWebApi(Mvc4)にhtmlの敏捷性を使用しています。これはGet Method Logicです
//GET api/values
public string GetHtmlFlights()
{
var result = ClientFlightTabale.GetXHtmlFromUri2("http://ikiafids.ir/departureFA.html");
HtmlNode node = result.DocumentNode.SelectSingleNode("//table[1]/tbody/tr[1]");
string temp = node.FirstChild.InnerHtml.Trim();
return temp;
}
しかし、私はこのメソッド(ブラウザとフィドラーから)例外を検出したとき、このテーマで:
オブジェクト参照がオブジェクトのインスタンスに設定されていません。この例外はこの行に関係しています
string temp = node.FirstChild.InnerHtml.Trim();
誰でも私を助けてくれる?
私はあなたが次のようなものを探していると思う:
var result = ClientFlightTabale.GetXHtmlFromUri2("http://ikiafids.ir/departureFA.html");
var tableNode = result.DocumentNode.SelectSingleNode("//table[1]");
var titles = tableNode.Descendants("th")
.Select(th => th.InnerText)
.ToList();
var table = tableNode.Descendants("tr").Skip(1)
.Select(tr => tr.Descendants("td")
.Select(td => td.InnerText)
.ToList())
.ToList();
私はあなたのセレクターが間違っていると思います。代わりにこれを試してください?
result.DocumentNode.SelectSingleNode("//table/tr[1]")