回答
如果你要做的整個頁面解析,那麼你可以嘗試HTML Agility pack。如果你需要的只是標題,那麼一些正則表達式就可以做到。
由於大部分時間標題都位於<title>
標記中,因此您可以直接提取該標記。
用於下載HTML,那麼你可以使用WebClient或HttpRequest /響應對象
個人而言,我喜歡和使用SgmlReader解析HTML:
using System;
using System.IO;
using System.Net;
using System.Xml;
using Sgml;
class Program
{
static void Main()
{
var url = "http://www.stackoverflow.com";
using (var reader = new SgmlReader())
using (var client = new WebClient())
using (var streamReader = new StreamReader(client.OpenRead(url)))
{
reader.DocType = "HTML";
reader.WhitespaceHandling = WhitespaceHandling.All;
reader.CaseFolding = Sgml.CaseFolding.ToLower;
reader.InputStream = streamReader;
var doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.XmlResolver = null;
doc.Load(reader);
var title = doc.SelectSingleNode("//title");
if (title != null)
{
Console.WriteLine(title.InnerText);
}
}
}
}
你想用在發現WebClient的對象System.Net.WebClient命名空間。
using System.Net;
使用WebClient,你可以下載整個網站作爲一個字符串,然後做任何你想要的字符串。 :)
例子:
WebClient client = new WebClient();
string content = wc.DownloadString("http://www.google.com");
然後,只需解析字符串反正你想要它。 :)在這個例子中,你可能希望找到標題元素,提取標題是這樣的:
string title = content.Substring(content.IndexOf("<title>"), content.IndexOf("</title>") - content.IndexOf("<title>")).Replace("<title>", "").Trim();
希望它能幫助。 :)
Thanku你@ peterthegreat.This這個片段真的幫助我。提前感謝 – 2011-03-05 15:36:07
很高興我能幫上忙。 :) – peterthegreat 2011-03-05 17:02:33
- 1. 從ListBox中讀取XML文件內容C#win表格
- 2. 如何在c中讀取html表格#
- 3. 反應呈現html表格和從html表格中讀取
- 4. 從PHP的HTML表格讀取數據
- 5. 從HTML中讀取並插入表格
- 6. 本地化C#Win表格
- 7. C#:從網站讀取HTML
- 8. 使用readHTML從https網頁讀取表格HTML表格
- 9. c# - 讀取HTML?
- 10. 在C++中讀取「表格」
- 11. 從列表框保存到c#Win中的一個.csv文件Win表格
- 12. 使用JavaScript或HTML從Google表格讀取單元格
- 13. 從xsd獲取html表格
- 14. 讀取空值並從xml文件返回null C#win from
- 15. 從C讀取Lua表
- 16. 讀取文件並寫入html表格
- 17. 使用HTML表格讀取時出錯
- 18. 如何使用Rselenium讀取html表格?
- 19. MS Reports rdlc c#win表格多列
- 20. C# - 讀取和顯示HTML從資源
- 21. 從註冊表中讀取Vista/win上的用戶7
- 22. unicode datagridview win表格
- 23. 從C#中的標籤列表中讀取和寫入表格
- 24. 如何從提示輸入框中獲取返回值? C#win表格
- 25. 從MySQL數據庫中讀取數據到HTML表格中?
- 26. 從HTML頁面讀取固定寬度格式的文本表
- 27. 從C++的Excel中讀取單元格?
- 28. 從C++中用空格讀取文件
- 29. 在C中讀取excel表單元格#
- 30. 使用C#讀取Word 2007表格
thnx爲你快速回復。 – 2011-03-05 15:18:53