私は、示すが、私はいくつかの問題HTMLAgilityPackを使用してHTMLデータを解析する
public class Book
{
public HtmlAttribute Href{ get; set; }
public string Title{ get; set; }
public string Author{ get; set; }
public string Characters{ get; set; }
}
これは私が解析しようとしているページですを得ただろう、私は、リンク、時々説明と文字リスト(hrefの値を必要としますそこに何も)ではありません:
<div id=title>
<li>
<h3><a href="www.harrypotter.com">Harry Potter</a></h3>
<div>Harry James Potter is the title character of J. K. Rowling's Harry Potter series. </div>
<ul>
<li>Harry Potter</li>
<li>Hermione Granger</li>
<li>Ron Weasley</li>
</ul>
</li>
<li>
<h3><a href="www.littleprince.com">Little Prince</a></h3>
<div>A little girl lives in a very grown-up world with her mother, who tries to prepare her for it. </div>
</li>
</div>
そして、これはそれを解析して、リストにそれを置くために私のコードです
List<Book> BookList= new List<Book>();
var titleNode = doc.DocumentNode.SelectNodes("//*[@id=\"title\"]//li//h3");
var descNode = doc.DocumentNode.SelectNodes("//*[@id=\"title\"]//li//div");
var authorNode = doc.DocumentNode.SelectNodes("//*[@id=\"title\"]//li//ul");
var title = titleNode.Select(node => node.InnerText).ToList();
var desc = descNode.Select(node => node.InnerText).ToList();
var characters= authorNode.Select(node => node.InnerText).ToList();
for (int i = 0; i < Title.Count(); ++i)
{
var list= new Book();
list.Title= title[i];
list.Author= desc[i];
list.Characters = characters[i];
BookList.Add(list);
}
私の質問は次のとおりです。1)href値を取得してリストに追加するにはどうすればよいですか? 2)HTMLの中に文字のタグがないものもありますが、NullReferenceExceptionエラーが発生してもリストを取得するにはどうすればよいですか?注:私はhtmlを変更することはできません。
私が解析しようとしているウェブサイトからのものであるため、htmlを変更できません。 – Blake