使用Google CustomsearchService在网络上搜索图片

时间:2017-05-30 12:15:11

标签: c# google-image-search

我需要一点帮助。当我按原样搜索网页时,下面的代码会发生故障,但是当我想搜索图像时

        const string apiKey = "xxxx";
        const string searchEngineId = "xxxx";
        const string query = "apple";
        var customSearchService = new CustomsearchService(new BaseClientService.Initializer { ApiKey = apiKey });
        var listRequest = customSearchService.Cse.List(query);
        listRequest.Cx = searchEngineId;
        listRequest.Num = 1;
        //listRequest.Fields = "items(image(contextLink,thumbnailLink),link)";
        listRequest.SearchType = CseResource.ListRequest.SearchTypeEnum.Image;
        listRequest.Start = 1;

        var search = listRequest.Execute();

        foreach (var result in search.Items)
        {
            Debug.WriteLine(string.Format("Title: {0}", result.Title));
            Debug.WriteLine(string.Format("Link: {0}", result.Link));
        }
search.Itemsnull时,

SearchType会产生Image

1 个答案:

答案 0 :(得分:0)

我很抱歉。我忘记在Google CSE页面上打开图像serch。现在它工作正常。