此代碼的工作的Html敏捷包,的SelectSingleNode
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
html = client.DownloadString("http://www.imdb.com/chart/moviemeter?ref_=nv_mv_mpm_8");
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
MessageBox.Show(doc.DocumentNode.SelectSingleNode("//*[@id='main']/div/span/div/div/div[3]/table/tbody/tr[1]/td[2]/a").InnerText);
的Html代碼在這裏:
<a href="/title/tt4972582/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=2240084082&pf_rd_r=1QW31NGD6JSE46F79CKQ&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=moviemeter&ref_=chtmvm_tt_1" title="M. Night Shyamalan (dir.), James McAvoy, Anya Taylor-Joy">Split</a>
MessageBox中顯示,這是 「拆分」 的文字。但是看看這個HTML代碼:
<div class="summary_text" itemprop="description">
Three girls are kidnapped by a man with a diagnosed 23 distinct personalities, and must try and escape before the apparent emergence of a frightful new 24th.
</div>
我想MessageBox中顯示哪些開頭的文字「三個女孩是kidn ......」所以我寫了這個代碼:
WebClient client2 = new WebClient();
client2.Encoding = Encoding.UTF8;
HtmlAgilityPack.HtmlDocument doc2 = new HtmlAgilityPack.HtmlDocument();
doc2.LoadHtml(client2.DownloadString("http://www.imdb.com/title/tt4972582/?pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=2240084082&pf_rd_r=1QW31NGD6JSE46F79CKQ&pf_rd_s=center-1&pf_rd_t=15506&pf_rd_i=moviemeter&ref_=chtmvm_tt_1"));
MessageBox.Show(doc2.DocumentNode.SelectSingleNode("//*[@id='title - overview - widget']/div[3]/div[1]/div[1]").InnerText);
當我開始這個代碼,發生類型爲「System.NullReferenceException」的未處理的異常
Xpaths是真的,我已經檢查了一百次,所以我該怎麼辦?
爲什麼你不使用特定的xpath而不是通過另一個?由於該xpath至少在此刻與您想要的元素不匹配。說明不在該div內。 –
我怎樣才能達到這個描述?你可以幫我嗎? – Onur