首页 > 解决方案 > 使用 API 和 Python 从 Virustotal 检索 IP 位置

问题描述

我有一个 excel 文件,其中包含每个 IP(同一行)的 Ips 和 API 密钥列表,以使用 API 集成从 Virustotal 获取它们的位置。

以下代码适用于一个 IP,但如何检查多个 IP?并获取国家/地区键/值

  import requests
    
    url = 'https://www.virustotal.com/vtapi/v2/ip-address/report'
    
    params = {'apikey':'<apikey>','ip':'<ip>'}
    
    response = requests.get(url, params=params)
    
    print(response.json())


Below is my current code .. which reads text file that looks like this( as dictionary )

**Text File:**

apikey #### IP 204.79.197.212
apikey #### IP 204.79.197.212

**Code :** 

url = 'https://www.virustotal.com/vtapi/v2/ip-address/report' # targeted url
# use this for IP https://www.virustotal.com/vtapi/v2/ip-address/report
with open('IP.json', 'w') as json_file:
    with open('IP.txt') as f:# In this file I have my api key for each domain 
        for line in f:
            counter += 1
            tok = line.split()
            d[tok[0]] = tok[1] # assign values to dict key 
            d[tok[2]] = str(tok[3]) #assign values to dict key  , 2 keys apikey which is fixed and other key can be domain , ip , etc        
            response = requests.get(url, params=d) # pass the dict 
            print(d) # just to print the dict , it works 
            #print(response.json())
            #json.dump(response.json(), json_file)
            #jvar=response.json()
            #print(jvar)
            #print(type(jvar))
            with open('IPCSV.csv', 'a', encoding="utf-8") as csv_file:
                writer = csv.writer(csv_file, delimiter='>')
                for k,v in jvar.items():
                    print(k,v)
                    if k == 'country':
                        print(k,v)
                        writer.writerow([k, v])

谢谢

标签: pythonapiautomation

解决方案


推荐阅读