10

I'm interested in automatizing reverse image search. Yandex in particular is great for busting catfishes, even better than Google Images. So, consider this Python code:

import requests
import webbrowser

try:
    filePath = "C:\\path\\whateverThisIs.png"
    searchUrl = 'https://yandex.ru/images/'
    multipart = {'encoded_image': (filePath, open(filePath, 'rb')), 'image_content': ''}
    response = requests.post(searchUrl, files=multipart, allow_redirects=False)
    #fetchUrl = response.headers['Location']
    print(response)
    print(dir(response))
    print(response.content)
    input()
except Exception as e:
    print(e)
    print(e.with_traceback)
    input()```

The script fails with KeyError, 'location' is not found. I know the code works cause if you substitute searchUrl with http://www.google.hr/searchbyimage/upload then the script returns the correct url. So, in short the expected outcome would be a url with an image search. In actuality we get a KeyError where that url was supposed to be stored. Evidently, Yandex doesn't work in exactly the same way, maybe the url is off (although I tried a heap ton of variations) or the reason may be completely different.

Regardless of that, help in solving this problem is much appreciated!

2
  • 3
    You need to replicate the http requests that your browser does. Use your browsers' Network Monitor to see what requests are made. As you pointed out, it doesn't work exactly like Google. It makes a POST request to a URL with many parameters, like https://yandex.com/images/search?serpid=ntjh1wqE0ZzVt [...] which returns json. Of particular interest is the key ["blocks"][0]["params"]["url"], the value of which you add to the base URL, resulting in something like yandex.com/images/…. That's ultimately the URL you want. Commented May 26, 2020 at 10:53
  • Did you try a HEAD or OPTIONS request to see what's going on at their backend (please share the response headers for these if you can), or also did try 'https://yandex.ru/images' instead of 'https://yandex.ru/images/'?
    – user11553043
    Commented Jun 1, 2020 at 3:49

3 Answers 3

15
+50

You can get url with an image search by using this code. Tested on ubuntu 18.04, with python 3.7 and requests 2.23.0

import json

import requests

file_path = "C:\\path\\whateverThisIs.png"
search_url = 'https://yandex.ru/images/search'
files = {'upfile': ('blob', open(file_path, 'rb'), 'image/jpeg')}
params = {'rpt': 'imageview', 'format': 'json', 'request': '{"blocks":[{"block":"b-page_type_search-by-image__link"}]}'}
response = requests.post(search_url, params=params, files=files)
query_string = json.loads(response.content)['blocks'][0]['params']['url']
img_search_url = search_url + '?' + query_string
print(img_search_url)
4
  • Works on Windows 7, with Python 3.6.8 and requests 2.18.4
    – Rufat
    Commented Jun 1, 2020 at 17:56
  • 1
    Is failing on Win10, Python 3.6.6, requests 2.24:c:\Python36>python yi_search.py Traceback (most recent call last): File "yi_search.py", line 10, in <module> query_string = json.loads(response.content)['blocks'][0]['params']['url'] KeyError: 'blocks'
    – Dave
    Commented Jun 26, 2020 at 16:38
  • Would you be so kind to explain me what this 'request': '{"blocks....." payload is for. By the way, your codes works perfectly on OSx Catalina, Python 3.7, requests 2.27.1 Commented Feb 22, 2022 at 20:25
  • @IvanCalderon That bit of code specifies which "blocks" should be returned in the payload. If you observe the traffic of Yandex itself, several blocks are requested. But in the case of this answer, only the search by image link block is requested.
    – Sampson
    Commented Jul 2, 2022 at 23:00
0

There are no API for developers. You can try reverse inginer queries from your browser, but you will have to deal with anty robot protect.

Another way to speed up process (but still manual)

  1. As described here https://yandex.com/support/images/loaded-image.html install Yandex.Browser where you have hot key for image search
  2. Host/make your site with all source images foe search queries
  3. Open your site in Yandex.Browser use "right mouse click"+"serch image at yandex"
  4. Copy what you need from page with results
0

If you try to inspect the Yandex requests, you can find the direct link to get Yandex Reverse Images results. The URL should look like:

https://yandex.com/images/search?url=link_to_your_image&rpt=imageview

From there, you will get the HTML content results. You can parse the HTML to JSON.

There are a lot of customized parameters: color, type, size, width, recent,.... You need to inspect, test all of them to make sure this work.

That is the process we built Yandex Reverse Images results. If you want an API, feel free to give SerpApi a try. I wrote a simple blog post: https://serpapi.com/blog/how-to-scrape-yandex-reverse-image/

I'm happy to share more in-depth technical solutions if you're curious. Feel free to ask.

Disclaimer: I worked at SerpApi.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.