【问题标题】:Reverse search an image in Yandex Images using Python使用 Python 反向搜索 Yandex 图像中的图像
【发布时间】:2020-09-10 15:40:58
【问题描述】:

我对自动进行反向图像搜索很感兴趣。尤其是 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'。我知道代码有效,因为如果你用http://www.google.hr/searchbyimage/upload 替换searchUrl,那么脚本会返回正确的url。 因此,简而言之,预期的结果将是一个带有图像搜索的 url。实际上,我们在应该存储该 url 的位置得到一个 KeyError。 显然,Yandex 的工作方式并不完全相同,可能是网址已关闭(尽管我尝试了很多变体),或者原因可能完全不同。

无论如何,非常感谢帮助解决这个问题!

【问题讨论】:

  • 您需要复制浏览器执行的 http 请求。使用浏览器的网络监视器查看发出的请求。正如您所指出的,它的工作方式与谷歌不同。它向带有许多参数的 URL 发出 POST 请求,例如返回 json 的https://yandex.com/images/search?serpid=ntjh1wqE0ZzVt [...]。特别感兴趣的是键 ["blocks"][0]["params"]["url"],您将其值添加到基本 URL,从而生成类似于 yandex.com/images/… 的内容。这最终就是您想要的 URL。
  • 您是否尝试了HEADOPTIONS 请求以查看他们的后端发生了什么(如果可以,请分享这些响应标头),或者也尝试'https://yandex.ru/images' 而不是'https://yandex.ru/images/'?

标签: python parsing web-crawler yandex


【解决方案1】:

您可以使用此代码通过图像搜索获取 url。适用于 ubuntu 18.04,使用 python 3.7 和 requests 2.23.0

import json

import requests

filePath = "C:\\path\\whateverThisIs.png"
searchUrl = 'https://yandex.ru/images/search'
files = {'upfile': ('blob', open(filePath, 'rb'), 'image/jpeg')}
params = {'rpt': 'imageview', 'format': 'json', 'request': '{"blocks":[{"block":"b-page_type_search-by-image__link"}]}'}
response = requests.post(searchUrl, params=params, files=files)
query_string = json.loads(response.content)['blocks'][0]['params']['url']
img_search_url= searchUrl + '?' + query_string
print(img_search_url)

【讨论】:

  • 适用于 Windows 7,使用 Python 3.6.8 并请求 2.18.4
  • 在 Win10、Python 3.6.6 上失败,请求 2.24:c:\Python36>python yi_search.py​​ Traceback(最近一次调用最后一次):文件“yi_search.py​​”,第 10 行,在 query_string = json.loads(response.content)['blocks'][0]['params']['url'] KeyError: 'blocks'
【解决方案2】:

没有面向开发人员的 API。您可以尝试从浏览器中进行反向查询,但您必须处理anty robot protection。

另一种加快进程的方法(但仍然是手动的)

  1. 如此处所述 https://yandex.com/support/images/loaded-image.html安装 Yandex.Browser 有图片搜索热键的地方
  2. 使用所有源图片来对抗搜索查询来托管/制作您的网站
  3. 在 Yandex.Browser 中打开您的网站,使用“鼠标右键”+“在 yandex”
  4. 从带有结果的页面中复制您需要的内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多