2016-11-28 30 views
-3

我需要從網站獲取源代碼,該網站採用框架結構。將HTML源代碼保存爲WinForms應用程序中的字符串

我已經有一個Windows窗體應用程序,其WebBrowser功能集成到它。
當我做一個右鍵單擊並選擇「查看源代碼」時,它會打開一個新的文本文檔,其中包含我需要的信息。

我已經試過webBrowser.Document,webBrowser.DocumentTextwebBrowser.DocumentStream,但所有這些只給我其他信息,我不需要。

該網站不是靜態的(這是一個聊天),它不會做會話,因此我不能使用Webclient.DownloadFile
我需要持續連接到網站幾個小時,而無需刷新網站。我沒有看到在Windows Forms中使用webBrowser的方法。

根據要求,這是網站,我說的是:http://server2.webkicks.de/stackoverflow-test/
您可以通過在第三個文本框中填寫一些用戶名作爲訪客登錄。

+1

發佈□請之前做一些研究。 – Tatranskymedved

+3

可能的重複[如何在C#中下載HTML源代碼](http://stackoverflow.com/questions/599275/how-can-i-download-html-source-in-c-sharp) – Tatranskymedved

+0

爲什麼不你只需使用'HttpClient'從wesbite下載? https://www.dotnetperls.com/httpclient –

回答

1

當你希望得到的動態HTML內容,並webBrowser.DocumentwebBrowser.DocumentTextwebBrowser.DocumentStream不工作,你的願望。

這裏的技巧:您可以隨時從C#運行您的自定義JavaScript代碼。這裏是你如何能得到當前的HTML你WebBrowser控制:

webBrowser.Document.InvokeScript("eval", new string[]{"document.body.outerHTML"}); 

參考How to inject Javascript in WebBrowser control?

更新

對於iframedocument裏面,你可以嘗試以下方法:

webBrowser.Document.InvokeScript("eval", new string[]{"document.querySelector(\"iframe\").contentWindow.document.documentElement.outerHTML"}); 

另一個更新

當你的網站包含frame,而不是iframe,這裏是你如何獲得該的html內容:

webBrowser.Document.InvokeScript("eval", new string[]{"document.querySelector(\"frame[name='mainframe'\").contentWindow.document.documentElement.outerHTML"}); 

最終測試和更新工作

querySelectorWebControl工作。因此,解決方法是:爲您的<frame>提供一些id,並使用該id獲取該<frame>元素。這裏是你如何實現你的任務。

HtmlElement frame = webBrowser1.Document.GetElementsByTagName("frame").Cast<HtmlElement>().FirstOrDefault(m => m.GetAttribute("name") == "mainframe"); 
if (frame != null) 
{ 
    frame.Id = "RandID_" + DateTime.Now.Ticks; 
    string html = webBrowser1.Document.InvokeScript("eval", new string[] { "document.getElementById('" + frame.Id + "').contentWindow.document.documentElement.outerHTML" }).ToString(); 
    Console.WriteLine(html); 
} 
else 
{ 
    MessageBox.Show("Frame not found"); 
} 
+0

感謝您的回答。雖然這確實給了我html源代碼,但它不是我正在尋找的那個。我想我需要框架的源代碼,我正在看。 雖然注入Javascript是最好的方法,正如你所建議 – NotTelling

+0

@TristanB。你的問題。在任何地方都不會說'Iframe'。不用擔心,我正在更新iframe的答案。 – sam

+0

對不起。我無法將我的問題納入技術術語,因爲我不是專業人員,而是學習者。謝謝! – NotTelling

0

如果您的網站的目標使用SSL協議(HTTPS),您可以嘗試添加用戶代理是這樣的:

using (WebClient myWebClient = new WebClient()) 
          { 
           myWebClient.Headers.Add("User-Agent: Other");    
           myWebClient.DownloadFile(new System.Uri("https://mywebsite.com//somefile"), "D:\\temp\\somefile"); 
          } 

如果您的網站的目標需要登錄,然後您登錄到您的websitetarget在Chrome和使用EditThisCookie擴展複製你的Cookie,並嘗試這一個:

using (WebClient myWebClient = new WebClient()) 
          { 
           myWebClient.Headers.Add("User-Agent: Other"); 
           myWebClient.Headers.Add(HttpRequestHeader.Cookie, "mycookies copies from EditThisCookie"); 
           myWebClient.DownloadFile(new System.Uri("https://mywebsite.com//somefile"), "D:\\temp\\somefile"); 
          } 
+0

感謝您的回答。在我的情況下,我不需要下載一個文件,但保持不斷跟蹤快速變化的HTML。爲了甚至到達那個html,我正在尋找,有問題的網站需要打開。如果我關閉它,我將需要重新登錄。 您的答案適用於這些條件嗎? – NotTelling

+0

對於您的問題的第一部分,您可以嘗試DownloadString而不是DownloadFile,然後嘗試在其中添加一段時間(true)和一個Thread.Sleep(2000),這意味着您將每隔2000ms檢查一次目標頁面的內容 –

+0

對於第二部分,cookie有一個到期日期,這意味着如果它過期了,您將無法再獲取目標頁面的內容,因此您得到的唯一解決方案就是手動完成,再次登錄,複製您的cookies並將其插入到您的Web客戶端頁眉上。 –

相關問題