如何使用C#将网页下载到txt文件中? [英] How to download a webpage into a txt file using C#?

查看:166
本文介绍了如何使用C#将网页下载到txt文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要下载网页的文字内容并使用C#将其保存到txt文件。



什么我试过了:



我是编码的初学者,我用google搜索并发现用C#创建和编写文件。但我不能'获取用于将网页下载到txt文件的c#教程。

I need to download the text content of a webpage and save it to a txt file using C#.

What I have tried:

I am a beginner in coding, I have googled and found someways to create and write a file using C#.But i couldn't get the c# tutorial for downloading a webpage into a txt file.

推荐答案

请参阅 WebClient.DownloadString Method(String)(System.Net) [ ^ ]。

它获取网站的内容到一个可以写入文件的字符串。



但你应该使用 .htm .html 文件扩展名,因为网站是HTML。如果您希望网站内容为纯文本,则必须使用HTML到文本转换器。
See the WebClient.DownloadString Method (String) (System.Net)[^].
It gets the content of a web site into a string which can be written to a file.

But you should use the .htm or .html file extension because web sites are HTML. If you want the content of the web site as plain text, you have to use a HTML to text converter.


这很简单:

It's pretty simple:
WebClient wc = new WebClient();
string html = wc.DownloadString(URL);
File.WriteAllText(path, html);


这篇关于如何使用C#将网页下载到txt文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆