【问题标题】:Web Search API from Contextual Web Search Engine来自上下文 Web 搜索引擎的 Web 搜索 API
【发布时间】:2018-11-06 05:21:24
【问题描述】:

如何在 C# 或 Python 中解析从上下文 Web 搜索 api 返回的 JSON 响应? (这里是请求的链接:http://contextualwebsearch.com/freeapi

【问题讨论】:

  • 你的问题是如何解析 JSON?

标签: api search full-text-search bing-api bing-search


【解决方案1】:

这是使用 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));
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多