2017-01-09 34 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源代码,因为这就是你要求的... –

相关问题