3

我正在尝试进行反向图像搜索并收集结果的描述。我很快就陷入了死胡同:

import requests

r = requests.get('http://www.google.com/searchbyimage?image_url=http://i.imgur.com/j3shP.jpg')
r.text

理想情况下,我应该能够找到维基百科的描述:“酒喉蜂鸟(Atthis ellioti)是蜂鸟科的一种蜂鸟。它在萨尔瓦多、危地马拉、洪都拉斯和......”。我可以在 Firebug 中手动找到它,但 GET 请求不会返回它。我做错了吗,或者这是谷歌不允许的功能?

4

1 回答 1

3

有趣的问题。出于某种原因,此功能需要一个 User-Agent 标头,至少在我的 Firefox 浏览器中,它不起作用。但是,如果您这样做:

headers = {}
headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"
r = requests.get('http://www.google.com/searchbyimage?image_url=http://i.imgur.com/j3shP.jpg', headers=headers)

有用。你会得到很多文字,但你可以看到一些对蜂鸟图像的引用:

h9fVq52EM&amp;tbnh=0&amp;tbnw=0&amp;prev=/search%3Fq%3D%26tbm%3Disch%26tbo%3Du&amp;zoom=1&amp;q=&amp;usg=__ZgK5jfDBcK04SU2bfmNK5FCj6t8=&amp;docid=xg1LDwTq_rx2VM&amp;sa=X&amp;ei=cFu6UIPgI-7q0QHi54DYAQ&amp;ved=0CH0Q1Rc"><img src="http://t3.gstatic.com/images?q=tbn:ANd9GcRZymll5oKgx8oqrdojY6ATda9pY5yWzG0JzsGcf7AA739px2wEYy_jYA" alt="Wine-throated Hummingbird" border=0 height=116 style="margin-left:-30px;" width=173></a></div><a href="/imgres?imgurl=http://ibc.lynxeds.com/files/imagecache/photo_940/pictures/Eisermann_Atthis_ellioti.jpg&amp;imgrefurl=http://ibc.lynxeds.com/photo/wine-throated-hummingbird-atthis-ellioti/displaying-male&amp;h=162&amp;w=242&amp;sz=5&amp;tbnid=3xjh-h9fVq52EM&amp;tbnh=0&amp;tbnw=0&amp;prev=/search%3Fq%3D%26tbm%3Disch%26tbo%3Du&amp;zoom=1&amp;q=&amp;usg=__ZgK5jfDBcK04SU2bfmNK5FCj6t8=&amp;docid=xg1LDwTq_rx2VM&amp;sa=X&amp;ei=cFu6UIPgI-7q0QHi54DYAQ&amp;ved=0CH4QuhM"class="fl ellip" style="color:#999;font-size:11px;display:inline-block;text-align:right;width:100%">ibc.lynxeds.com</a></div><div class=krable data-ved="0CH0Q1Rc" style="float:right;margin:5px 0 4px"></div></div><div class="kno-desc kno-fb-ctx"><div >The Wine-Throated Hummingbird is a species of hummingbird in the Trochilidae family. It is found in El Salvador, Guatemala, Honduras, and Mexico. Its natural habitats are subtropical or tropical moist montane forests. <a href="http://en.wikipedia.org/wiki/Wine-throated_Hummingbird" class="fl q" style="color:#999;font-size:11px"  onmousedown="return rwt(this,\'\',\'\',\'\',\'17\',\'AFQjCNH07EpIAh31_2ubTIQThRNfQluC8g\',\'\',\'0CH8QmhMoADAQ\',\'\',\'\',event)"><span class=kno-desca>Wikipedia</span></a></div><div class=krable data-ved="0CH8QmhM" style="margin:2px 0 4px"></div></div><div class=rhsl5 style=clear:both></div><div class="kno-ft kno-xs"><table class="kno-fs ts"><tr><td style="vertical-align:top;width:100%"><div class="kno-f kno-fb-ctx"><span class=krable data-ved="0CIIBEMsTKAA" style="margin:0 0 5px"></span><span class="kno-fh "><a href="/search?hl=en&amp;tbo=d&amp;q=wine+throated+hummingbird+scientific+name&amp;sa=X&amp;ei=cFu6UIPgI-7q0QHi54DYAQ&amp;ved=0CIEBEOgT" class=fl style=color:#000>Scientific name</a>: </span><span class="kno-fv"><span class="kno-fv-vq fl" data-vq="/search?hl=en&amp;tbo=d&amp;q=%22wine-throated+hummingbird%22+%22scientific+name%22+%22atthis+ellioti%22&amp;stick=H4sIAAAAAAAAAAEtANL_AHvTx-gAAAAQCAMiCi9tLzAydnRyNnJAAcWxzJB1fpUio0i895QopmaQvkAE23M00C0AAAA">Atthis ellioti</span></span></div><div class="kno-f kno-fb-ctx"><span class=krable data-ved="0CIQBEMsTKAE" style="margin:0 0 5px"></span>
于 2012-12-01T19:35:56.170 回答