2013-10-14 21 views
-2

我想知道我怎麼可以從網頁如何查找和在C#中提取網頁文本

例如獲取數據:

<li id="hello1">about me 
    <ul class="square"> 
     <li><strong>name: john</strong></li> 
    </ul> 
</li> 

我要在名字前面閱讀約翰:那麼我是怎麼着在C#閱讀 哦,我曾嘗試使用HTML Agility Pack :(但由於其較差的文檔,我是不能夠使用這樣需要幫助。

+5

可以請你告訴我們,你已經嘗試? –

+0

我的意思是我已經下載了HTML敏捷包,但是我沒有在文檔文件中找到任何示例,以便我瞭解它的用法 – user776046

回答

2

使用HtmlAgilityPack

HtmlDocument doc = new HtmlDocument(); 
doc.Load(yourStream); 
var nameElement= doc.DocumentNode.SelectSingleNode("//li[@id='hello1']").InnerText; 
//name would contain `about me name: john` 
Regex.Match(nameElement,@"(?<=name:\s*)\w+").Value;//john 
0

我已經使用HTML敏捷性包之前,它是偉大的工具

HtmlDocument document = new HtmlDocument(); 

document.LoadHtml(YourHTML); 
var collection = document.DocumentNode.SelectNodes("//li[@id='hello1']");