使用 Python 在 Yandex Images 中反向搜索图像

2024-07-04

我对自动化反向图像搜索感兴趣。 Yandex 特别适合捕捞鲶鱼,甚至比 Google 图片还要好。因此,请考虑以下 Python 代码:

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()```

脚本失败并出现 KeyError,'location'没有找到。我知道代码可以工作,因为如果你替换searchUrl with http://www.google.hr/searchbyimage/upload然后脚本返回正确的 url。 因此,简而言之,预期结果将是带有图像搜索的 url。实际上,我们在应该存储该 url 的地方收到了一个 KeyError 。 显然,Yandex 的工作方式并不完全相同,也许 url 已关闭(尽管我尝试了很多变体),或者原因可能完全不同。

不管怎样,非常感谢帮助解决这个问题!


您可以使用此代码通过图像搜索获取 url。在 ubuntu 18.04、python 3.7 和 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Python 在 Yandex Images 中反向搜索图像 的相关文章

随机推荐