Skip to content

Instantly share code, notes, and snippets.

@miodeqqq
Created September 19, 2019 13:22
Show Gist options
  • Select an option

  • Save miodeqqq/00b22fe893a6aedbc1feac67e17a184f to your computer and use it in GitHub Desktop.

Select an option

Save miodeqqq/00b22fe893a6aedbc1feac67e17a184f to your computer and use it in GitHub Desktop.
Search image with Google
import os
import requests
from user_agent import generate_user_agent
# pip install user_agent requests
img_path = os.path.join('/path/to/your/image')
def search_image_with_google(img_path):
"""
Search image with Google
"""
url = r'https://www.google.com/searchbyimage/upload'
with requests.Session() as s:
s.cookies.clear()
files = {
'encoded_image': (img_path, open(img_path, 'rb')),
'image_content': ''
}
s.headers.update(**{'User-Agent': generate_user_agent()})
r = s.post(
url=url,
files=files,
allow_redirects=False
)
return r.headers.get('Location', 'not_found')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment