リクエストに対するHTTP GETレスポンスは以下のようなものです
<html>
<head> <script type="text/javascript">----</script> <script type="text/javascript">---</script> <title>Detailed Notes</title>
</head>
<body style="background-color: #FFFFFF; border-width: 0px; font-family: sans-serif; font-size: 13; color: #000000"> <p>this is one note </p> </body> </html>
私はこれを文字列として取得しており、それから身体の部分を読み取る必要があります。
HtmlAgilityパックを試してみましたが、HTMLコンテンツに特別なものがあるため、HTML解析が失敗します(この問題の原因はコメント付きスクリプトにあると思います)。
タグの内容を読むために、私はSubString操作を考えています。
<body tag
の先頭からのSubStringのような
。
テキストから単語の先頭からSubStringを実行するにはどうすればよいですか?
回答 2 件
リクエストソースを取得した後、「問題を引き起こすため、すべてを置き換える必要があります」
WebClient client = new WebClient(); // make an instance of webclient string source = client.DownloadString("url").Replace("\"",",,"); // get the html source and escape " with any charachter string code = "<body style=\"background-color: #FFFFFF; border-width: 0px; font-family: sans-serif; font-size: 13; color: #000000\"> <p>this is one note </p> </body>"; MatchCollection m0 = Regex.Matches(code, "(<body)(?<body>.*?)(</body>)", RegexOptions.Singleline); // use RE to get between tags foreach (Match m in m0) // loop through the results { string result = m.Groups["body"].Value.Replace(",,", "\""); // get the result and replace the " back }
シンプルな
SubString()
を使用するIndexOf
と ()+LastIndexOf()
:これは戻ります:
<p> this is one note </p>
これは戻ります:
<body style = background-color: #FFFFFF; border-width: 0px; font-family: sans-serif; font-size: 13; color: #000000' >< p > this is one note </p> </body>