如何在Google中搜索时从第一页获取图片? [英] How to get the Image from first page when search in Google?

查看:270
本文介绍了如何在Google中搜索时从第一页获取图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在使用Google搜索城市后,右侧会有维基百科页面的一部分,其中包含图片和地图。任何人都可以告诉我如何访问此图像?我应该知道如何下载它。

Usually after using Google to search for a city, there is a part of Wikipedia page on the right with an image and a map. Can anyone tell me how I could access this image? I should know how to download it.

推荐答案

实际上主图像(与右边的地图图像一起)非常很少来自维基百科,所以你不能使用维基百科API来获取它。如果你想访问实际的主图像,你可以使用它:

Actually the main image (that goes with the map image on the right) is very rarely from Wikipedia, so you can't use Wikipedia API to get it. If you want to access the actual main image you can use this:

private static void GetGoogleImage(string word)
{
    // make an HTTP Get request
    var request = (HttpWebRequest)WebRequest.Create("https://www.google.com.pg/search?q=" + word);
    request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36";
    using (var webResponse = (HttpWebResponse)request.GetResponse())
    {
        using (var reader = new StreamReader(webResponse.GetResponseStream()))
        {
            // get all images with base64 string
            var matches = Regex.Matches(reader.ReadToEnd(), @"'data:image/jpeg;base64,([^,']*)'");
            if (matches.Count > 0)
            {
                // get the image with the max height
                var bytes = matches.Cast<Match>()
                    .Select(x => Convert.FromBase64String(x.Groups[1].Value.Replace("\\75", "=").Replace("\\075", "=")))
                    .OrderBy(x => Image.FromStream(new MemoryStream(x, false)).Height).Last();

                // save the image as 'image.jpg'
                using (var imageFile = new FileStream("image.jpg", FileMode.Create))
                {
                    imageFile.Write(bytes, 0, bytes.Length);
                    imageFile.Flush();
                }
            }
        }
    }
}

这项工作对我来说,并且总是返回实际的主图像(如果存在的话)。例如, GetGoogleImage(纽约)给我 data:image / jpeg; base64,/ 9j / 4AAQSkZJRg ......

This work for me, and always returns the actual main image (if such exists). For example, GetGoogleImage("New York") give me data:image/jpeg;base64,/9j/4AAQSkZJRg....

我使用的事实是从所有base64字符串中的图像响应主具有最大高度,因此只需按高度排序并选择最后一个。如果需要,您也可以在此处查看最小图像高度。需要将 \ 075 替换为 = base64的填充

I use the fact that from the all base64 string images in response the main has the max height, so its need only to order them by height and to select the last one. If it's required, you can check here also for minimum image height. The replacing \075 to = is needed base64's padding.

这篇关于如何在Google中搜索时从第一页获取图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆