当前位置: 动力学知识库 > 问答 > 编程问答 >

c# How to read the content of a website as text?

问题描述:

How to get the content of a site?

The site im talking about is just a simple page that doesn't have any other links on it:

take this as example: http://introcs.cs.princeton.edu/java/23recursion/Queens.java.html

I just want to read the content and save it on a string so later on I can manipulate it easily.

Any idea? or tutorial?

Thanks in advance : )

网友答案:
using(System.Net.WebClient wc = new System.Net.WebClient()) {
    MessageBox.Show(wc.DownloadString("http://thewebsite.com/thepage.html")); // Or whatever
}
网友答案:

I need more clarification, unless I'm just being dense.

Are you wanting to capture the output of this URL from the command line?

If so, you could use

wget -q "http://introcs.cs.princeton.edu/java/23recursion/Queens.java.html" -O captured_file.txt

Again, forgive me if I completely misunderstand the request...

网友答案:

You can use System.Net.WebClient class methods.

网友答案:

Like this?

/// ---- ScreenScrape --------------------------------
///
/// <summary>
/// Input: URL like  "http://www.microsoft.com"
/// Return: HTML string of site
/// </summary>

static public String ScreenScrape(String URL)
{
    WebRequest TheRequest = WebRequest.Create(URL);
    WebResponse TheResponse = TheRequest.GetResponse();

    StreamReader TheStreamReader = new StreamReader(TheResponse.GetResponseStream(), Encoding.UTF8);

    String HTML = TheStreamReader.ReadToEnd();

    return HTML;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: