【问题标题】:How to get console value or <div> by ID from HTML如何从 HTML 通过 ID 获取控制台值或 <div>
【发布时间】:2016-05-24 19:18:06
【问题描述】:

我正在尝试在 Visual-studio 2015 中使用 C# 来检索控制台或通过 .HTML 文件上的 ID="show"&lt;div&gt; 获取值。

这是我的 C# 代码

using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
    public static void Main()
    {
        // Create a request for the URL.        
        WebRequest request = WebRequest.Create("http://localhost:8080/trymyown/computeArea.html");

        // If required by the server, set the credentials.
        request.Credentials = CredentialCache.DefaultCredentials;

        // Get the response.
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);

        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();

        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);

        // Read the content.
        string responseFromServer = reader.ReadToEnd();

        // Display the content.
        Console.WriteLine(responseFromServer);

        // Cleanup the streams and the response.
        reader.Close();
        dataStream.Close();
        response.Close();

        Console.ReadLine();
    }
}
}

这是我的 .HTML 文件:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">

<title>computeArea</title>

  </head>
  <body>

<div id="show"> </div>

<script>
    // This example creates a simple polygon representing the Bermuda Triangle.
    // When the user clicks on the polygon an info window opens, showing
    // information about the polygon's coordinates.

    var map;
    var build;

    function initMap() {            
      // Define the LatLng coordinates for the polygon.
      var RoundList = [
          {lat: 25.774, lng: -80.190},
          {lat: 18.466, lng: -66.118},
          {lat: 18.400, lng: -65.118},
          {lat: 19.551, lng: -60.164},
          {lat: 32.321, lng: -64.757}
      ];

      // Construct the polygon.
      build = new google.maps.Polygon({
        paths: RoundList
      });
    }

    function showArea()
    {

var area = parseFloat(google.maps.geometry.spherical.computeArea(build.getPath())) * 0.000247105; //this
        area = area.toFixed(4);
        document.getElementById("show").innerHTML = area;

        console.log(area);

        //document.write("Area = ");
        //document.write(area);
    }
</script>

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCVK8oA9vvEBK0CSSvD8haB5i1QYreHmmA&callback=initMap"></script>

<script>
    setTimeout(function(){ showArea(); }, 100);
</script>

  </body>
</html>

但是我遇到了一些问题! 我只能得到所有的 HTML“代码”。

谁能帮我弄清楚?

【问题讨论】:

  • 发布您的代码而不是图片。
  • 尝试探索和使用`Html Agility Pack'
  • 不好意思,这是我第一次使用,马上改
  • 您当前的实现没有考虑到您想要的div 的内容是由客户端js 生成的。您需要使用一个(浏览器)组件来执行客户端 js,而不仅仅是下载 html 页面源代码。
  • @Filburt 我只想让 c# 由 c++ 调用然后从 HTML 中获取值,我想知道我应该创建哪种项目,C# Windows 窗体应用程序或 C# 控制台应用程序?谢谢!!

标签: c# html visual-studio


【解决方案1】:

不确定我是否正确理解了这个问题,但我认为您想提取 div 的内容,请使用以下内容:

string strRegex = "<div\\sid=\"show\">(.*?)</div>";
if (System.Text.RegularExpressions.Regex.IsMatch(responseFromServer, strRegex))
{
Console.WriteLine(System.Text.RegularExpressions.Regex.Match(responseFromServer, strRegex).Value.Replace("<div\\sid=\"show\">", "").Replace("</div>", ""));
 }

希望这是您正在寻找的。​​p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2013-08-20
    • 1970-01-01
    相关资源
    最近更新 更多