Google使用POST请求进行反向图片搜索 [英] Google reverse image search using POST request

查看:181
本文介绍了Google使用POST请求进行反向图片搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,基本上是存储在本地驱动器上的图像数据库.有时,我需要找到图像的高分辨率版本或网络来源,然后使用Google的反向图像搜索是最理想的选择.

I have an app that's basically a database of images stored on my local drive. Sometimes I need to find a higher resolution version or the web source of an image, and Google's reverse image search is ideal for that.

不幸的是,Google没有它的API,因此我不得不想出一种手动完成此操作的方法.现在我正在使用Selenium,但是显然有很多开销.我想要一个使用urllib2或类似方法的简单解决方案-发送POST请求,找回搜索URL,然后可以将该URL传递给webbrowser.open(url),以将其加载到已经打开的系统浏览器中.

Unfortunately, Google doesn't have an API for it, so I had to figure out a way to do it manually. Right now I'm using Selenium, but that obviously has a lot of overhead. I'd like a simple solution using urllib2 or something similar - send a POST request, get the search URL back, and then I can just pass that URL to webbrowser.open(url) to load it in my already opened system browser.

这是我现在正在使用的内容:

Here's what I'm using right now:

gotUrl = QtCore.pyqtSignal(str)
filePath = "/mnt/Images/test.png"

browser = webdriver.Firefox()
browser.get('http://www.google.hr/imghp')

# Click "Search by image" icon
elem = browser.find_element_by_class_name('gsst_a')
elem.click()

# Switch from "Paste image URL" to "Upload an image"
browser.execute_script("google.qb.ti(true);return false")

# Set the path of the local file and submit
elem = browser.find_element_by_id("qbfile")
elem.send_keys(filePath)

# Get the resulting URL and make sure it's displayed in English
browser.get(browser.current_url+"&hl=en")
try:
    # If there are multiple image sizes, we want the URL for the "All sizes" page
    elem = browser.find_element_by_link_text("All sizes")
    elem.click()
    gotUrl.emit(browser.current_url)
except:
    gotUrl.emit(browser.current_url)
browser.quit()

推荐答案

如果您愿意安装

This is easy to do if you're happy to install the requests module. The reverse image search workflow currently consists of a single POST request with a multipart body to an upload URL, the response to which is a redirect to the actual results page.

import requests
import webbrowser

filePath = '/mnt/Images/test.png'
searchUrl = 'http://www.google.hr/searchbyimage/upload'
multipart = {'encoded_image': (filePath, open(filePath, 'rb')), 'image_content': ''}
response = requests.post(searchUrl, files=multipart, allow_redirects=False)
fetchUrl = response.headers['Location']
webbrowser.open(fetchUrl)

当然,请记住Google可能会决定随时更改此工作流程!

Of course, remember that Google may decide to change this workflow at any point!

这篇关于Google使用POST请求进行反向图片搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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