是什么导致这个基本的 dhash 查询 VirusTotal API Python 示例代码持续产生 404 错误?

问题描述 投票:0回答:1

刚开始使用 VirusTotal API 并弄清楚这一点将为更多人打开大门。我期待 dhash 价值回报。即使将“MY_API_KEY”更新为直接从我的 VirusTotal 配置文件复制的密钥,以下代码仍会持续导致以下错误:

检索 dhash 值时出错:404 - {"error":{"code":"NotFoundError","message":"Resource not found."}}


import requests
# Replace YOUR_API_KEY with your actual VirusTotal API key
API_KEY = "MY_API_KEY"
DOMAIN = "google.com"
# Construct the URL to request the dhash value for the domain
url = f"https://www.virustotal.com/api/v3/domains/{DOMAIN}/dhash"
# Set the API headers
headers = {"x-apikey": API_KEY}
# Make the API request
response = requests.get(url, headers=headers)
# Check if the request was successful
if response.status_code == 200:
    # Get the dhash value from the response
    dhash = response.json()["data"]["id"]
    print(f"The dhash value for {DOMAIN} is: {dhash}")
else:
    print(f"Error retrieving dhash value: {response.status_code} - {response.text}")
python api
1个回答
0
投票

在线 APIv3 包括示例代码生成器,用于检索一组域属性作为 json 对象,其中包括付费用户 API 访问的“dhash”值(不是免费的 API 密钥)。 链接:https://developers.virustotal.com/reference/domain-info

通过解析域对象属性检索破折号值的示例代码:

import requests
# return list of values matching key in json object with recursive search
def find_key(dictionary, target_key):
# Check if the target key is in the current dictionary
    if target_key in dictionary:
        return dictionary[target_key]
    else:
        # Recursively search any nested dictionaries
        for value in dictionary.values():
            if isinstance(value, dict):
                result = find_key(value, target_key)
                if result is not None:
                    return result
        # If the target key is not found, return None
         return None

base_url= "https://www.virustotal.com/api/v3/domains/"

# update to your VirusTotal API key 
API_KEY = "0000000000000000000000000000000000000000000000000000000000000000" 

headers = {
    "accept": "application/json",
    "x-apikey": API_KEY }

response = requests.get(base_url+"google.com", headers=headers)

# Parse the response text as a JSON object into a nested dictionary
data = response.json()

print(response.text)
print("dhash results #1 - direct reference:", data["data"]["attributes"]["favicon"]["dhash"])
print("dhash results #2 - recursive find_key lookup: ", 
find_key(data,"dhash"))

运行上述示例代码的结果摘录如下:

{
"data": {
    "attributes": {
       <snip> 

dhash 结果 #1 - 直接参考:e89e931939338ee8

dhash 结果 #2 - 递归 find_key 查找:e89e931939338ee8

© www.soinside.com 2019 - 2024. All rights reserved.