访问的网页的内容用C# [英] Access the Contents of a Web Page with C#
问题描述
我想用C#来访问一个网页的内容。例如,我想抓住谷歌的网页正文的文字。
I am trying to use C# to access the content of a webpage. For example, I want to grab the text of the body of google homepage.
我知道这是可行的在C#中,其网络浏览器的控制。但我找不到这样做的一个很好的,简单的例子。所有的资源,我在网上找到涉及创建表格和图形用户界面,我不需要,我只需要一个良好的老控制台应用程序。
I know this is doable in C# with its web browser control. But I couldn't find a good, simple example of doing it. All the resources I found online involve creating Forms and GUI, which I don't need, I just need a good old Console Application.
如果任何人都可以提供实现上述简单的基于控制台的code段,这将是极大的AP preciated。
If anyone can provide a simple console-based code snippet that accomplishes the above, it'll be greatly appreciated.
推荐答案
其实web浏览器就是你要显示一个网页(嵌入在Windows应用程序管理Internet Explorer)的情况下使用的GUI控制。如果你只需要得到一个网页的内容,你可以使用的 Web客户端类:
Actually the WebBrowser is a GUI control used in case you want to visualize a web page (embed and manage Internet Explorer in your windows application). If you just need to get the contents of a web page you could use the WebClient class:
class Program
{
static void Main(string[] args)
{
using (var client = new WebClient())
{
var contents = client.DownloadString("http://www.google.com");
Console.WriteLine(contents);
}
}
}
这篇关于访问的网页的内容用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!