来自上下文网络搜索引擎的网络搜索 API
Web Search API from Contextual Web Search Engine
如何在 C# 或 Python 中解析从上下文 Web 搜索 api 返回的 JSON 响应?
(这里是请求的 link:http://contextualwebsearch.com/freeapi)
这是使用 API using the RapidAPI marketplace.
的 C# 代码
/// <summary>
/// Performs a Web search and return the results as a SearchResult.
/// </summary>
private static void MaShapeParsingExample()
{
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
//Step 1. Replace the following string value with your valid X-Mashape-Key key.
string Your_X_Mashape_Key = "OV5vB1qRFnmsh2GYXgVtmjbIfLzup1JXrVjjsntqzb3T25JWCA";
//Step 2. The query parametrs:
int count = 10; //the number of items to return
string q = "Donald Trump"; //the search query
bool autoCorrect = true; //autoCorrectspelling
//Step 3. Perform the Web request and get the response
var response = Unirest.get(string.Format("https://contextualwebsearch-websearch-v1.p.mashape.com/api/Search/WebSearchAPI?q={0}&count={1}&autocorrect={2}", q, count, autoCorrect))
.header("X-Mashape-Key", Your_X_Mashape_Key)
.header("X-Mashape-Host", "contextualwebsearch-websearch-v1.p.mashape.com")
.asJson<string>();
//Step 4. Get the ResponseBody as a JSON
dynamic jsonBody = JsonConvert.DeserializeObject(response.Body);
//Step 5. Parse the results
//Get the numer of items returned
int totalCount = (int)jsonBody["totalCount"];
//Get the list of most frequent searches related to the input search query
List<string> relatedSearch = JsonConvert.DeserializeObject<List<string>>(jsonBody["relatedSearch"].ToString());
//Go over each resulting item
foreach (var webPage in jsonBody["value"])
{
//Get The web page metadata
string url = webPage["url"].ToString();
string title = webPage["title"].ToString();
string description = webPage["description"].ToString();
string keywords = webPage["keywords"].ToString();
string provider = webPage["provider"]["name"].ToString();
DateTime datePublished = DateTime.Parse(webPage["datePublished"].ToString());
//Get the web page image (if exists)
string imageUrl = webPage["image"]["url"].ToString(); //get the webpage image url
int imageHeight = (int)webPage["image"]["height"]; //get the webpage image height
int widthHeight = (int)webPage["image"]["width"]; //get the webpage image width
//An example: Output the webpage url, title and published date:
Console.WriteLine(string.Format("Url: {0}. Title: {1}. Published Date:{2}.",
url,
title,
datePublished));
}
}
如何在 C# 或 Python 中解析从上下文 Web 搜索 api 返回的 JSON 响应? (这里是请求的 link:http://contextualwebsearch.com/freeapi)
这是使用 API using the RapidAPI marketplace.
的 C# 代码 /// <summary>
/// Performs a Web search and return the results as a SearchResult.
/// </summary>
private static void MaShapeParsingExample()
{
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
//Step 1. Replace the following string value with your valid X-Mashape-Key key.
string Your_X_Mashape_Key = "OV5vB1qRFnmsh2GYXgVtmjbIfLzup1JXrVjjsntqzb3T25JWCA";
//Step 2. The query parametrs:
int count = 10; //the number of items to return
string q = "Donald Trump"; //the search query
bool autoCorrect = true; //autoCorrectspelling
//Step 3. Perform the Web request and get the response
var response = Unirest.get(string.Format("https://contextualwebsearch-websearch-v1.p.mashape.com/api/Search/WebSearchAPI?q={0}&count={1}&autocorrect={2}", q, count, autoCorrect))
.header("X-Mashape-Key", Your_X_Mashape_Key)
.header("X-Mashape-Host", "contextualwebsearch-websearch-v1.p.mashape.com")
.asJson<string>();
//Step 4. Get the ResponseBody as a JSON
dynamic jsonBody = JsonConvert.DeserializeObject(response.Body);
//Step 5. Parse the results
//Get the numer of items returned
int totalCount = (int)jsonBody["totalCount"];
//Get the list of most frequent searches related to the input search query
List<string> relatedSearch = JsonConvert.DeserializeObject<List<string>>(jsonBody["relatedSearch"].ToString());
//Go over each resulting item
foreach (var webPage in jsonBody["value"])
{
//Get The web page metadata
string url = webPage["url"].ToString();
string title = webPage["title"].ToString();
string description = webPage["description"].ToString();
string keywords = webPage["keywords"].ToString();
string provider = webPage["provider"]["name"].ToString();
DateTime datePublished = DateTime.Parse(webPage["datePublished"].ToString());
//Get the web page image (if exists)
string imageUrl = webPage["image"]["url"].ToString(); //get the webpage image url
int imageHeight = (int)webPage["image"]["height"]; //get the webpage image height
int widthHeight = (int)webPage["image"]["width"]; //get the webpage image width
//An example: Output the webpage url, title and published date:
Console.WriteLine(string.Format("Url: {0}. Title: {1}. Published Date:{2}.",
url,
title,
datePublished));
}
}