我有一个程序,基本上是一个存储在本地驱动器上的图像数据库。有时我需要找到更高分辨率的版本或图像的网络来源,而Google的反向图像搜索很适合这个用途。
不幸的是,Google没有为此提供API,所以我不得不手动找到一种方法来实现它。目前我正在使用Selenium,但显然有很多额外的开销。我想要一个简单的解决方案,使用urllib2或类似的工具 - 发送POST请求,获取搜索URL,然后我可以将该URL传递给webbrowser.open(url)
,以在我的已打开系统浏览器中加载它。
以下是我目前正在使用的代码:
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()
www.google.com/searchbyimage?image_url=IMAGE_URL
会很有用。 - Others