【问题标题】:Assign div from a website to a string将网站中的 div 分配给字符串
【发布时间】:2017-03-26 15:14:01
【问题描述】:
                string locationName = Console.ReadLine();
                string url = "https://www.google.com/#q=latitude+and+longitude+" + locationName;

                HtmlWeb web = new HtmlWeb();
                HtmlDocument doc = web.Load(url);
                HtmlNode rateNode = doc.DocumentNode.SelectSingleNode("//div[@class='_XWk']");
                string res = rateNode.InnerText;
                Console.WriteLine(res);

我正在使用上面的代码从 google 获取某个位置,并将显示纬度和经度的文本框复制到字符串 res 中。每次运行代码时,我都会收到 nullReferenceException。

我怎样才能把字符串分成两个只有坐标的字符串?

http://imgur.com/a/7useZ

String res = "34.0522° N, 118.2437° W" 转换为 String res1 = "34.0522"String res2 = "118.2437"

提前致谢

【问题讨论】:

    标签: c# html string web html-agility-pack


    【解决方案1】:

    这会很困难,因为您要查找的内容不在链接的 HTML 代码中:https://www.google.com/#q=latitude+and+longitude+Paris

    它看起来像是 ajax 注入编码自:https://www.google.com/search?q=latitude+and+longitude+paris&bav=on.2,or.r_cp.&cad=b&fp=1&biw=1920&bih=677&dpr=1&tch=1&ech=1&psi=

    \\x3cdiv class\\x3d\\x22_XWk\\x22\\x3e48.8566\\xb0 N, 2.3522\\xb0 E\\x3c\/div\\x3e
    

    获取城市经度和纬度的更好方法是使用 Google Map API:

    https://maps.googleapis.com/maps/api/geocode/json?address=Paris

    【讨论】:

      【解决方案2】:

      您可以使用 Google Map Geocoding API。此 API 将向您发送一个 Json 文件。 像这样使用 newtonsoft Json 库:

          String fileName = "LosAngeles";
          WebRequest webRequest = WebRequest.Create("http://maps.google.com/maps/api/geocode/json?address=" + fileName);
          WebResponse response = webRequest.GetResponse();
          using (Stream responseStream = response.GetResponseStream())
          {
              StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
              String json = reader.ReadToEnd();
              JObject jsonObject = JObject.Parse(json);
              String lat = (string)jsonObject["results"][0]["geometry"]["location"]["lat"];
              String lng = (string)jsonObject["results"][0]["geometry"]["location"]["lng"];
              Console.WriteLine(lat + " : " + lng);
      
          }
      

      【讨论】:

        【解决方案3】:

        一种简单的方法是从 NuGet 安装库或在包管理器控制台中编写

        Install-Package GoogleMaps.LocationServices
        

        安装软件包后,由于使用了它的内置功能,您可以轻松获取 lat 和 log

         static void Main(string[] args)
                {
                    string locationName = Console.ReadLine();
        
                    var location = new GoogleLocationService();
                    var point = location.GetLatLongFromAddress(locationName);
        
                    Console.WriteLine(point.Latitude);
                    Console.WriteLine(point.Longitude);
                    Console.Read();
        
                }
        

        【讨论】:

          猜你喜欢
          • 2017-10-03
          • 1970-01-01
          • 1970-01-01
          • 2015-05-26
          • 1970-01-01
          • 2015-09-15
          • 2010-10-09
          • 1970-01-01
          相关资源
          最近更新 更多