Created
September 19, 2019 13:22
-
-
Save miodeqqq/00b22fe893a6aedbc1feac67e17a184f to your computer and use it in GitHub Desktop.
Search image with Google
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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