为何调用VirusTotal API查询dhash的Python示例代码持续返回404错误?
解决VirusTotal API获取域名dhash返回404的问题
刚接触VirusTotal API,想获取域名的dhash值,但替换真实API密钥后,代码仍返回以下错误:
Error retrieving dhash value: 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}")
问题原因
VirusTotal API v3不存在/domains/{DOMAIN}/dhash这个独立端点,你访问的路径无效,因此返回404错误。域名的dhash值包含在域名详情的截图属性中,需要先获取域名完整信息,再从中提取对应值。
修正后的代码
import requests API_KEY = "你的实际API密钥" DOMAIN = "google.com" # 正确的域名详情请求端点 url = f"https://www.virustotal.com/api/v3/domains/{DOMAIN}" headers = {"x-apikey": API_KEY} response = requests.get(url, headers=headers) if response.status_code == 200: domain_data = response.json() # 提取截图数据(部分域名可能无截图) screenshots = domain_data["data"]["attributes"].get("screenshots", []) if screenshots: # 取第一张截图的dhash值 dhash = screenshots[0]["dhash"] print(f"{DOMAIN}的dhash值为: {dhash}") else: print(f"{DOMAIN}无可用截图数据,无法获取dhash值") else: print(f"获取域名信息失败: {response.status_code} - {response.text}")
注意事项
- 并非所有域名都有截图数据,若目标域名未被VirusTotal抓取过截图,会返回空列表,无法获取dhash。
- 免费版API存在请求频率限制,频繁请求可能触发限流。
内容的提问来源于stack exchange,提问作者user3298772
相关产品推荐
相关产品推荐

