如何通过 API 从维基百科获取地标地点的标题?
How to get title of landmark place from Wikipedia by API?
对于我的代码,我使用维基百科 API,它提供指向与该城市的维基百科文章链接的所有地方的链接。但是我的代码有一些额外的不必要的链接。我只想 return 类型为 "landmark".
的链接
我的维基百科 API 是:
https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Berlin&gslimit=500&gsprop=type|name|dim|country|region|globe&format=json
示例 JSON 来自维基百科的数据 API:
"query": {
"geosearch": [
{
"pageid": 5858187,
"ns": 0,
"title": "Stuttgart Hauptbahnhof",
"lat": 48.783888888889,
"lon": 9.1816666666667,
"dist": 136.8,
"primary": "",
"type": "railwaystation",
"name": "",
"dim": 1000,
"country": "DE",
"region": "BW"
},
{
"pageid": 6102287,
"ns": 0,
"title": "Staatstheater Stuttgart",
"lat": 48.780277777778,
"lon": 9.185,
"dist": 361,
"primary": "",
"type": "landmark",
"name": "",
"dim": "900",
"country": "DE",
"region": "BW"
},
{
"pageid": 35806545,
"ns": 0,
"title": "Versatel building",
"lat": 48.78409,
"lon": 9.17799,
"dist": 400.4,
"primary": "",
"type": null,
"name": "",
"dim": 1000,
"country": null,
"region": null
},
{
"pageid": 3230957,
"ns": 0,
"title": "Neue Staatsgalerie",
"lat": 48.780277777778,
"lon": 9.1869444444444,
"dist": 430.6,
"primary": "",
"type": "landmark",
"name": "",
"dim": 1000,
"country": "DE",
"region": "BW"
},
....
]
}
我的代码从这个 API 中获取 Title
。
using (var client = new HttpClient())
{
var response = client.GetAsync("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Berlin&gslimit=500&gsprop=type|name|dim|country|region|globe&format=json").Result;
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content;
string responseString = responseContent.ReadAsStringAsync().Result;
var obj = JsonConvert.DeserializeObject<RootObject>(responseString).query.geosearch.Select(a => a.title).ToList();
foreach (var item in obj)
{
Console.WriteLine(item);
}
}
}
这是当前输出:
如何从 type
为 "landmark" 的结果中获取标题?
我不懂 C#:-)
但是尝试这样的事情:
var geosearch = JsonConvert.DeserializeObject<RootObject>(responseString).query.geosearch;
var landmarks = geosearch.Where(type => type == "landmark");
如果您使用 JSON 格式,试试这个:
var obj = JsonConvert.DeserializeObject<RootObject>(responseString).query.geosearch
.Where(a => a.type == "landmark").Select(a => a.title).ToList();
也可以不使用Json.NET获得所有称号。这就是我使用 XML 格式的方式:
using (var webResponse = (HttpWebResponse)WebRequest.Create("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Berlin&gslimit=500&gsprop=type&format=xml").GetResponse())
{
using (var reader = new StreamReader(webResponse.GetResponseStream()))
{
var response = XElement.Parse(reader.ReadToEnd());
var obj = response.Descendants("gs")
.Where(a => a.Attribute("type") != null && a.Attribute("type").Value == "landmark")
.Select(a => a.Attribute("title").Value).ToList();
}
}
对于我的代码,我使用维基百科 API,它提供指向与该城市的维基百科文章链接的所有地方的链接。但是我的代码有一些额外的不必要的链接。我只想 return 类型为 "landmark".
的链接我的维基百科 API 是:
https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Berlin&gslimit=500&gsprop=type|name|dim|country|region|globe&format=json
示例 JSON 来自维基百科的数据 API:
"query": {
"geosearch": [
{
"pageid": 5858187,
"ns": 0,
"title": "Stuttgart Hauptbahnhof",
"lat": 48.783888888889,
"lon": 9.1816666666667,
"dist": 136.8,
"primary": "",
"type": "railwaystation",
"name": "",
"dim": 1000,
"country": "DE",
"region": "BW"
},
{
"pageid": 6102287,
"ns": 0,
"title": "Staatstheater Stuttgart",
"lat": 48.780277777778,
"lon": 9.185,
"dist": 361,
"primary": "",
"type": "landmark",
"name": "",
"dim": "900",
"country": "DE",
"region": "BW"
},
{
"pageid": 35806545,
"ns": 0,
"title": "Versatel building",
"lat": 48.78409,
"lon": 9.17799,
"dist": 400.4,
"primary": "",
"type": null,
"name": "",
"dim": 1000,
"country": null,
"region": null
},
{
"pageid": 3230957,
"ns": 0,
"title": "Neue Staatsgalerie",
"lat": 48.780277777778,
"lon": 9.1869444444444,
"dist": 430.6,
"primary": "",
"type": "landmark",
"name": "",
"dim": 1000,
"country": "DE",
"region": "BW"
},
....
]
}
我的代码从这个 API 中获取 Title
。
using (var client = new HttpClient())
{
var response = client.GetAsync("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Berlin&gslimit=500&gsprop=type|name|dim|country|region|globe&format=json").Result;
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content;
string responseString = responseContent.ReadAsStringAsync().Result;
var obj = JsonConvert.DeserializeObject<RootObject>(responseString).query.geosearch.Select(a => a.title).ToList();
foreach (var item in obj)
{
Console.WriteLine(item);
}
}
}
这是当前输出:
如何从 type
为 "landmark" 的结果中获取标题?
我不懂 C#:-)
但是尝试这样的事情:
var geosearch = JsonConvert.DeserializeObject<RootObject>(responseString).query.geosearch;
var landmarks = geosearch.Where(type => type == "landmark");
如果您使用 JSON 格式,试试这个:
var obj = JsonConvert.DeserializeObject<RootObject>(responseString).query.geosearch
.Where(a => a.type == "landmark").Select(a => a.title).ToList();
也可以不使用Json.NET获得所有称号。这就是我使用 XML 格式的方式:
using (var webResponse = (HttpWebResponse)WebRequest.Create("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=10000&gspage=Berlin&gslimit=500&gsprop=type&format=xml").GetResponse())
{
using (var reader = new StreamReader(webResponse.GetResponseStream()))
{
var response = XElement.Parse(reader.ReadToEnd());
var obj = response.Descendants("gs")
.Where(a => a.Attribute("type") != null && a.Attribute("type").Value == "landmark")
.Select(a => a.Attribute("title").Value).ToList();
}
}