how to copy all text from a certain webpage and save it to notepad C#

蹲街弑〆低调 提交于 2019-12-12 08:08:40

问题


I have a C# Windows Forms app that launches a webpage based on some criteria.

Now I would like my app to automatically copy all the text from that page (which is in CSV format) and paste and save it in notepad.

Here is a link to an example of the data that needs to be copied: http://www.wunderground.com/history/airport/FAJS/2012/10/28/DailyHistory.html?req_city=Johannesburg&req_state=&req_statename=South+Africa&format=1

Any Help will be appreciated.


回答1:


http://msdn.microsoft.com/en-us/library/fhd1f0sw.aspx combined with http://www.dotnetspider.com/resources/21720-Writing-string-content-file.aspx

public static void DownloadString ()
{
    WebClient client = new WebClient();
    string reply = client.DownloadString("http://www.wunderground.com/history/airport/FAJS/2012/10/28/DailyHistory.html?req_city=Johannesburg&req_state=&req_statename=South+Africa&format=1");

    StringBuilder stringData = new StringBuilder();
    stringData = reply;  
    FileStream fs = new FileStream(@"C:\Temp\tmp.txt", FileMode.Create);
    byte[] buffer = new byte[stringData.Length];
    for (int i = 0; i < stringData.Length; i++)
    {
        buffer[i] = (byte)stringData[i];
    }
    fs.Write(buffer, 0, buffer.Length);
    fs.Close();
}

Edit Adil uses the WriteAllText method, which is even better. So you will get something like this:

WebClient client = new WebClient();
string reply = client.DownloadString("http://www.wunderground.com/history/airport/FAJS/2012/10/28/DailyHistory.html?req_city=Johannesburg&req_state=&req_statename=South+Africa&format=1");
System.IO.File.WriteAllText (@"C:\Temp\tmp.txt", reply);



回答2:


You can use the new toy HttpClient from .NET 4.5, example how to get google page:

 var httpClient = new HttpClient();
 File.WriteAllText("C:\\google.txt",    
                           httpClient.GetStringAsync("http://www.google.com")
                                     .Result);  



回答3:


Simple way: use WebClient.DownloadFile and save as a .txt file:

var webClient = new WebClient();
webClient.DownloadFile("http://www.google.com",@"c:\google.txt");



回答4:


You need WebRequest to read the stream of and save to string to text file. You can use File.WriteAllText to write it to file.

WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
                    request.Credentials = CredentialCache.DefaultCredentials;            
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();            
Console.WriteLine (response.StatusDescription);            
Stream dataStream = response.GetResponseStream ();            
StreamReader reader = new StreamReader (dataStream);            
string responseFromServer = reader.ReadToEnd ();
System.IO.File.WriteAllText (@"D:\path.txt", responseFromServer );



回答5:


You may use a webclient to do this:

System.Net.WebClient wc = new System.Net.WebClient();
byte[] raw = wc.DownloadData("http://www.wunderground.com/history/airport/FAJS/2012/10/28/DailyHistory.html?req_city=Johannesburg&req_state=&req_statename=South+Africa&format=1");

string webData = System.Text.Encoding.UTF8.GetString(raw);

then the string webData contains the complete text of the webpage



来源:https://stackoverflow.com/questions/13118660/how-to-copy-all-text-from-a-certain-webpage-and-save-it-to-notepad-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!