2017-01-09 72 views
1

我想從C#中加載例如此頁(url)「http://finance.yahoo.com/q/ks?s=FORK+Key+Statistic」,然後將該頁保存爲文本文件以供稍後解析或抓取。我知道我可以通過瀏覽器(我的情況下是Firefox)通過右鍵單擊頁面,然後「將頁面另存爲...」來完成此操作,然後將其另存爲文本文件。然後,所有帶有我需要的數據的文本都將存儲在一個文本文件中供以後解析。我想知道如何從C#中自動執行此過程。我發現MSDN的代碼可以自動打印網頁:如何使用C#將網頁保存爲文本文件供以後解析

private void PrintHelpPage() 
{ 
    // Create a WebBrowser instance. 
    WebBrowser webBrowserForPrinting = new WebBrowser(); 

    // Add an event handler that prints the document after it loads. 
    webBrowserForPrinting.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(PrintDocument); 

    // Set the Url property to load the document. 
    webBrowserForPrinting.Url = new Uri(@"\\myshare\help.html"); 
} 

private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
    // Print the document now that it is fully loaded. 
    ((WebBrowser)sender).Print(); 

    // Dispose the WebBrowser now that the task is complete. 
    ((WebBrowser)sender).Dispose(); 
} 

這個工作原理除了只打印頁頭。有沒有人知道有一種方法可以像瀏覽器中的Save或Save Page As命令那樣執行大致相同的操作?我也嘗試了其他選項,如htmlAgilityPack,WebClient和htrpClient。這些方法都返回html源代碼,它不包含網頁上顯示的任何數據。如果我能找到如何找到網頁上的數據元素的位置ID,那也可能是有用的。

我終於得到它的工作(見下面的代碼):

 WebBrowser browser = new WebBrowser(); 
     browser.ScriptErrorsSuppressed = true; 
     int j = 0; 
     label1.Text = j.ToString(); 
     label1.Refresh(); 
     int SleepTime = 3000; 
     loadPage: browser.Navigate("http://finance.yahoo.com/q/ks?s=GBX+Key+Statistic"); 
     System.Threading.Thread.Sleep(SleepTime); 
     MessageBox.Show("browser.Navigae OK"); //Why is MessageBox needed here??? 
     label1.Refresh(); 
     if (browser.ReadyState == WebBrowserReadyState.Complete) 
     { 
      // It's done! 
      string path = @"C:\VS2015Projects\C#\caoStocksCS\textFiles\somefile13.txt"; 
      //MessageBox.Show("path OK"); 
      if (browser.Document.Body.Parent.InnerText != null) 
      { 
       File.WriteAllText(path, browser.Document.Body.Parent.InnerText, Encoding.GetEncoding(browser.Document.Encoding)); 
       MessageBox.Show("Success! somefile13.txt created"); 
      } 
      else 
      { 
       MessageBox.Show("browser.Document.Body.Parent.InnerText=" + browser.Document.Body.Parent.InnerText); 
       MessageBox.Show("Failure somefile13.txt not created"); 
      } 
     } 
     else 
     { 
      SleepTime += SleepTime; 
      ++j; 
      label1.Text = j.ToString(); 
      goto loadPage; 
     } 

但是,它不是完全自動化的,因爲MessageBox.Show( 「browser.Navigae OK」); //爲什麼在這裏需要MessageBox?或者在這裏需要其他一些消息框,否則它只是繼續前進。
有誰知道爲什麼需要MessageBox? 有沒有反正我可以做同樣的事情的MessageBox不需要在這裏調用消息框? MessageBox不會暫停系統,直到它被點擊或解散?有沒有什麼辦法可以在沒有消息框的情況下做到這一點?

回答

7

您可以嘗試使用WebClient.DownloadString。該方法下載指定的URL代碼並將其保存爲字符串。你可以查看MSDN上有關此https://msdn.microsoft.com/en-us/library/fhd1f0sw(v=vs.110).aspx

WebClient client = new WebClient(); 
string downloadString = client.DownloadString("http://finance.yahoo.com/q/ks?s=FORK+Key+Statistic"); 

然後,保存什麼......你下載,你可以方便地使用File.WriteAllText。無論何時您想要寫入文件的完整字符串(如此情況),此方法都非常適用:

File.WriteAllText("C:/yourWebPAge.txt", downloadString); 
+0

請添加一些關於此代碼爲何有助於OP的解釋。這將有助於提供未來觀衆可以從中學習的答案。有關更多信息,請參閱[答案]。 –

+0

@MikeMcCaughan你明白了 – NicoRiff

+0

我認爲你的評論是針對@NicoRiff而不是我的,因爲我只是要求這個答案的海報包含更多的信息,因爲「試試這個」的答案對其他人來說並不是很有幫助。關於你的評論,當然它包含HTML源代碼,因爲這就是你要求的... –

相關問題