c# - 在 .NET 和 C# 中从网站提取数据时出现问题

标签 c# asp.net httpwebresponse streamreader web-scraping

我编写了一个网页抓取程序来转到页面列表并将所有 html 写入文件。问题是,当我拉出一段文本时,某些字符被写成“�”。如何将这些字符提取到我的文本文件中?这是我的代码:

string baseUri = String.Format("http://www.rogersmushrooms.com/gallery/loadimage.asp?did={0}&blockName={1}", id.ToString(), name.Trim());

// our third request is for the actual webpage after the login.
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(baseUri);
request.Method = "GET";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)";
//get the response object, so that we may get the session cookie.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());

// and read the response
string page = reader.ReadToEnd();

StreamWriter SW;
string filename = string.Format("{0}.txt", id.ToString());
SW = File.AppendText("C:\\Share\\" + filename);

SW.Write(page);

reader.Close();
response.Close();

最佳答案

您正在将名为 loadimage 的页面保存到文本文件中。您确定这真的是所有文字吗?

无论哪种方式,您都可以使用 System.Net.WebClient.DownloadFile() 节省大量代码。

关于c# - 在 .NET 和 C# 中从网站提取数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3040545/

相关文章:

c# - 更新面板问题

c# - mvvmlight 信使奇怪的行为

c# - ASP.NET 中的页面刷新

c# - ASP.NET:根据计时器更改 .aspx 的背景图像

c# - 将 List<IInfrastructureEntity> 转换为 List<TEntity>,其中泛型类型 TEntity 必须实现该接口(interface)

c# - 声明变量只是为了使代码易于阅读,编程最佳实践 C#

c# - 如何将 MySqlDataReader 阅读器结果返回到 List<DateTime> mylist

c# - HttpListenerResponse 及其 ContentLength64 属性的无限值

c# - 如何等到使用 HttpWebRequest 的 Web 请求完成?

c# - WebException 响应属性返回 null