回答
如果你要做的整個頁面解析,那麼你可以嘗試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. 反應呈現html表格和從html表格中讀取
- 2. C#:從網站讀取HTML
- 3. 使用readHTML從https網頁讀取表格HTML表格
- 4. 從xsd獲取html表格
- 5. 從C讀取Lua表
- 6. 讀取文件並寫入html表格
- 7. 使用HTML表格讀取時出錯
- 8. MS Reports rdlc c#win表格多列
- 9. C# - 讀取和顯示HTML從資源
- 10. 從註冊表中讀取Vista/win上的用戶7
- 11. 從C#中的標籤列表中讀取和寫入表格
- 12. 從MySQL數據庫中讀取數據到HTML表格中?
- 13. 從C++中用空格讀取文件
- 14. 從html表單錯誤讀取輸入
- 15. 從HTML中讀取數據Javascript表單
- 16. 在C中讀取excel表單元格#
- 17. C++ CGI腳本,讀取表格數據
- 18. 使用C#讀取Word 2007表格
- 19. 使用pdftools從PDF中讀取表格
- 20. 從表格中讀取數據
- 21. 無法從PHP表格讀取$ _POST []值
- 22. Javascript不會從「select」中讀取表格
- 23. 從表格視圖中讀取數據
- 24. 定期從Google表格中讀取
- 25. 從電子表格中讀取值
- 26. 如何從.txt文件讀取表格至C++
- 27. 從網頁讀取表格的C++程序
- 28. 使用C#從Excel電子表格讀取/寫入#
- 29. 從文本文件中讀取並在C#表格中顯示
- 30. 如何使用OpenXMLSDK和C#從Excel表格中讀取圖像?
thnx爲你快速回復。 – 2011-03-05 15:18:53