0

.NET の WebRequest を使用して、自分のページを一時的なハックとして "スクリーン スクレイピング" しています。

これはうまく機能しますが、アクセント記号付きの文字と分音記号付きの文字は正しく変換されません。

.NET の多くの組み込みプロパティとメソッドを使用して、それらを正しく変換する方法があるかどうか疑問に思っています。

ページを取得するために使用しているコードは次のとおりです。

private string getArticle(string urlToGet)
{

    StreamReader oSR = null;

    //Here's the work horse of what we're doing, the WebRequest object 
    //fetches the URL
    WebRequest objRequest = WebRequest.Create(urlToGet);

    //The WebResponse object gets the Request's response (the HTML) 
    WebResponse objResponse = objRequest.GetResponse();

    //Now dump the contents of our HTML in the Response object to a 
    //Stream reader
    oSR = new StreamReader(objResponse.GetResponseStream());


    //And dump the StreamReader into a string...
    string strContent = oSR.ReadToEnd();

    //Here we set up our Regular expression to snatch what's between the 
    //BEGIN and END
    Regex regex = new Regex("<!-- content_starts_here //-->((.|\n)*?)<!-- content_ends_here //-->",
        RegexOptions.IgnoreCase);

    //Here we apply our regular expression to our string using the 
    //Match object. 
    Match oM = regex.Match(strContent);

    //Bam! We return the value from our Match, and we're in business. 
    return oM.Value;
}
4

2 に答える 2

2

使用してみてください:

System.Net.WebClient client = new System.Net.WebClient();
string html = client.DownloadString(urlToGet);
デコードされた文字列=System.Web.HttpUtility.HtmlDecode(html);

また、client.Encodingをチェックしてください

于 2009-04-29T23:25:21.437 に答える
0

次のように、StreamReader コンストラクターの 2 番目のパラメーターを使用して、これを処理する別の方法があります。

new StreamReader(webRequest.GetResponse().GetResponseStream(), 
                 Encoding.GetEncoding("ISO-8859-1"));

それはそれをするでしょう。

于 2010-07-08T19:18:18.497 に答える