将 div 从网站分配给字符串
Assign div from a website to a string
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。
我怎样才能将字符串分成两个只有坐标的字符串?
String res = "34.0522° N, 118.2437° W"
变成 String res1 = "34.0522"
和 String res2 = "118.2437"
提前致谢
这会很难,因为您要查找的内容不在 link 的 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 地图 API :
https://maps.googleapis.com/maps/api/geocode/json?address=Paris
您可以使用 Google 地图地理编码 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);
}
一种简单的方法是从 NuGet 安装库或在程序包管理器控制台中编写
Install-Package GoogleMaps.LocationServices
安装包后,由于使用其内置功能,您可以非常轻松地获取纬度和日志
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();
}
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。
我怎样才能将字符串分成两个只有坐标的字符串?
String res = "34.0522° N, 118.2437° W"
变成 String res1 = "34.0522"
和 String res2 = "118.2437"
提前致谢
这会很难,因为您要查找的内容不在 link 的 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 地图 API :
https://maps.googleapis.com/maps/api/geocode/json?address=Paris
您可以使用 Google 地图地理编码 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);
}
一种简单的方法是从 NuGet 安装库或在程序包管理器控制台中编写
Install-Package GoogleMaps.LocationServices
安装包后,由于使用其内置功能,您可以非常轻松地获取纬度和日志
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();
}