【问题标题】:how to fetch google images如何获取谷歌图片
【发布时间】:2011-02-12 12:30:02
【问题描述】:

我想针对任何查询获取谷歌图片。我已经通过谷歌图片搜索 api 但无法理解。我也看到了一些方法,它们只获取第一页的图像。我使用了以下方法。

函数 getGoogleImg($k) { $url = "http://images.google.it/images?as_q=##query##&hl=it&imgtbs=z&btnG=Cerca+con+Google&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype =&imgc=&as_sitesearch=&as_rights=&safe=图片&as_st=y"; $web_page = file_get_contents(str_replace("##query##",urlencode($k), $url)); $tieni = stristr($web_page,"dyn.setResults("); $tieni = str_replace("dyn.setResults(","", str_replace(stristr($tieni,");"),"",$tieni)); $tieni = str_replace("[]","",$tieni); $m = preg_split("/[\[\]]/",$tieni); $x = 数组(); for($i=0;$i

此函数仅返回 21 张图像。我想要针对此查询的所有图像。我在 php 中这样做

【问题讨论】:

  • 你有没有见过一个 google-resultpage 可以为你提供所有(可能是数百万)结果?但是,您最好使用 image-search-api :code.google.com/intl/de/apis/imagesearch
  • 是的,莫莉博士。但是当它返回时,它只返回其中的一些。不是全部。因为我们无法废弃 Google 图片。
  • 请给我看一个包含所有结果的结果页面(当然有超过 20 张图片)
  • 我怎么给你看?我使用了上述返回图像的函数。上面的函数只返回21张图片的src
  • Google 不允许以这种方式使用他们的搜索引擎。所有自动请求都应通过 API 执行。

标签: php image


【解决方案1】:

遗憾的是,图像 API 正在关闭,所以我不建议移至该 API,但我认为这会是一个更好的解决方案。

我最好的猜测是图像 22 和转发正在使用某种 som ajax/javascript 加载(如果您搜索说徽标并向下滚动,您会看到在向下移动时加载的占位符)并且您需要通过 javascript 引擎传递页面,这不是我能找到任何使用 php 的人(还)。 您是否检查过 $web_page 包含超过 21 张图片(当我玩弄谷歌图片搜索时,它使用 javascript 加载一些图片)? 当您从普通浏览器访问链接时会发生什么,如果您关闭 javascript 会发生什么? 您的结果中是否有指向下一页的链接?

在现已弃用的 Image API 中,有一些方法可以限制每页的结果数量以及跳转到下一页的方法https://developers.google.com/image-search/v1/jsondevguide#json_snippets_php

如果您希望继续进行搜索并从搜索结果中获取图像,那么稍后http://simplehtmldom.sourceforge.net/ 可能是一个不错的选择。 它获取一个 html DOM 并允许您轻松找到节点并使其易于使用。但它仍然使用 file_get_contents 或 curl 库来获取数据,因此它可能需要一些摆弄才能让 javascript 工作。

【讨论】:

    【解决方案2】:

    我写了一个脚本来从谷歌图片搜索中下载图片,我目前正在下载 100 张原始图片

    我在stackoverflow上写的原始脚本回答

    Python - Download Images from google Image search?

    我将详细解释我如何使用 urllib2 和 BeautifulSoup 从 Google 图片搜索中抓取原始图片的 url

    例如,如果你想从谷歌图像搜索中抓取电影终结者 3 的图像

    query= "Terminator 3"
    query=  '+'.join(query.split())  #this will make the query terminator+3
    url="https://www.google.co.in/search?q="+query+"&source=lnms&tbm=isch"
    header={'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"
    }
    req = urllib2.Request(url,headers=header)
    soup= urllib2.urlopen(req)
    soup = BeautifulSoup(soup)
    

    上面的变量汤包含请求页面的 html 代码,现在我们需要提取图像,您必须在浏览器中打开网页并检查图像上的元素

    在这里你会找到包含 url 图片的标签

    例如,对于谷歌图片,我发现 "div",{"class":"rg_meta"} 包含图片链接

    你可以搜索 BeautifulSoup 文档

    print soup.find_all("div",{"class":"rg_meta"})
    

    你会得到一个结果列表

    <div class="rg_meta">{"cl":3,"cr":3,"ct":12,"id":"C0s-rtOZqcJOvM:","isu":"emuparadise.me","itg":false,"ity":"jpg","oh":540,"ou":"http://199.101.98.242/media/images/66433-Terminator_3_The_Redemption-1.jpg","ow":960,"pt":"Terminator 3 The Redemption ISO \\u0026lt; GCN ISOs | Emuparadise","rid":"VJSwsesuO1s1UM","ru":"http://www.emuparadise.me/Nintendo_Gamecube_ISOs/Terminator_3_The_Redemption/66433","s":"Screenshot Thumbnail / Media File 1 for Terminator 3 The Redemption","th":168,"tu":"https://encrypted-tbn2.gstatic.com/images?q\\u003dtbn:ANd9GcRs8dp-ojc4BmP1PONsXlvscfIl58k9hpu6aWlGV_WwJ33A26jaIw","tw":300}</div>
    

    上面的结果包含指向我们图片网址的链接

    http://199.101.98.242/media/images/66433-Terminator_3_The_Redemption-1.jpg
    

    您可以按如下方式提取这些链接和图片

    ActualImages=[]# contains the link for Large original images, type of  image
    for a in soup.find_all("div",{"class":"rg_meta"}):
        link , Type =json.loads(a.text)["ou"]  ,json.loads(a.text)["ity"]
        ActualImages.append((link,Type))
    
    for i , (img , Type) in enumerate( ActualImages):
        try:
            req = urllib2.Request(img, headers={'User-Agent' : header})
            raw_img = urllib2.urlopen(req).read()
            if not os.path.exists(DIR):
                os.mkdir(DIR)
            cntr = len([i for i in os.listdir(DIR) if image_type in i]) + 1
            print cntr
            if len(Type)==0:
                f = open(DIR + image_type + "_"+ str(cntr)+".jpg", 'wb')
            else :
                f = open(DIR + image_type + "_"+ str(cntr)+"."+Type, 'wb')
    
    
            f.write(raw_img)
            f.close()
        except Exception as e:
            print "could not load : "+img
            print e
    

    瞧,现在你可以使用这个脚本从谷歌搜索中下载图像。或用于收集训练图像

    您可以在此处获取完整的脚本

    https://gist.github.com/rishabhsixfeet/8ff479de9d19549d5c2d8bfc14af9b88

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多