2012-08-09 32 views
5
import json 
import urllib 
import urllib2 
url = "https://www.virustotal.com/vtapi/v2/file/report" 
parameters = {"resource": "2aa837802b1c7867a65067525a843566029bd97e3ce99f6eb55217e219043ae1", 
     "apikey": "123123123123123123"} 
data = urllib.urlencode(parameters) 
req = urllib2.Request(url, data) 
response = urllib2.urlopen(req) 

json_object = json.load(response) 

print json_object['scans'] 

Các bạn có thể giúp bằng cách lấy giá trị cụ thể trong đối tượng JSON mà mã của tôi trả về không? Mã kết nối với VirusTotal để nhận kết quả quét đối tượng.Cách lấy giá trị/nội dung trong đối tượng JSON bằng python

Tôi đã cố in nó bằng dòng mã cuối cùng nhưng nó in toàn bộ đầu ra.

Đối tượng JSON trông như thế này:

{ 
    "scans": { 
     "TotalDefense": { 
      "detected": false, 
      "version": "37.0.10022", 
      "result": null, 
      "update": "20120809" 
     }, 
     "nProtect": { 
      "detected": false, 
      "version": "2012-08-09.02", 
      "result": null, 
      "update": "20120809" 
     }, 
     "CAT-QuickHeal": { 
      "detected": false, 
      "version": "12.00", 
      "result": null, 
      "update": "20120809" 
     }, 
     "McAfee": { 
      "detected": false, 
      "version": "5.400.0.1158", 
      "result": null, 
      "update": "20120809" 
     }, 
     "K7AntiVirus": { 
      "detected": false, 
      "version": "9.145.7456", 
      "result": null, 
      "update": "20120808" 
     }, 
     "TheHacker": { 
      "detected": false, 
      "version": "None", 
      "result": null, 
      "update": "20120808" 
     }, 
     "VirusBuster": { 
      "detected": false, 
      "version": "15.0.138.0", 
      "result": null, 
      "update": "20120809" 
     }, 
     "F-Prot": { 
      "detected": false, 
      "version": "4.6.5.141", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Symantec": { 
      "detected": false, 
      "version": "20121.1.0.298", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Norman": { 
      "detected": false, 
      "version": "6.08.06", 
      "result": null, 
      "update": "20120809" 
     }, 
     "ByteHero": { 
      "detected": false, 
      "version": "1.0.0.1", 
      "result": null, 
      "update": "20120723" 
     }, 
     "TrendMicro-HouseCall": { 
      "detected": false, 
      "version": "9.500.0.1008", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Avast": { 
      "detected": false, 
      "version": "6.0.1289.0", 
      "result": null, 
      "update": "20120809" 
     }, 
     "eSafe": { 
      "detected": false, 
      "version": "7.0.17.0", 
      "result": null, 
      "update": "20120808" 
     }, 
     "ClamAV": { 
      "detected": false, 
      "version": "0.97.3.0", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Kaspersky": { 
      "detected": false, 
      "version": "9.0.0.837", 
      "result": null, 
      "update": "20120809" 
     }, 
     "BitDefender": { 
      "detected": false, 
      "version": "7.2", 
      "result": null, 
      "update": "20120809" 
     }, 
     "ViRobot": { 
      "detected": false, 
      "version": "2011.4.7.4223", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Sophos": { 
      "detected": false, 
      "version": "4.80.0", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Comodo": { 
      "detected": false, 
      "version": "13190", 
      "result": null, 
      "update": "20120809" 
     }, 
     "F-Secure": { 
      "detected": false, 
      "version": "9.0.16440.0", 
      "result": null, 
      "update": "20120809" 
     }, 
     "DrWeb": { 
      "detected": false, 
      "version": "7.0.3.07130", 
      "result": null, 
      "update": "20120809" 
     }, 
     "VIPRE": { 
      "detected": false, 
      "version": "12556", 
      "result": null, 
      "update": "20120809" 
     }, 
     "AntiVir": { 
      "detected": false, 
      "version": "7.11.39.82", 
      "result": null, 
      "update": "20120809" 
     }, 
     "TrendMicro": { 
      "detected": false, 
      "version": "9.561.0.1027", 
      "result": null, 
      "update": "20120809" 
     }, 
     "McAfee-GW-Edition": { 
      "detected": false, 
      "version": "2012.1", 
      "result": null, 
      "update": "20120808" 
     }, 
     "Emsisoft": { 
      "detected": false, 
      "version": "5.1.0.11", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Jiangmin": { 
      "detected": false, 
      "version": "13.0.900", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Antiy-AVL": { 
      "detected": false, 
      "version": "2.0.3.7", 
      "result": null, 
      "update": "20120808" 
     }, 
     "Microsoft": { 
      "detected": false, 
      "version": "1.8601", 
      "result": null, 
      "update": "20120809" 
     }, 
     "SUPERAntiSpyware": { 
      "detected": false, 
      "version": "4.40.0.1006", 
      "result": null, 
      "update": "20120809" 
     }, 
     "GData": { 
      "detected": false, 
      "version": "22", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Commtouch": { 
      "detected": false, 
      "version": "5.3.2.6", 
      "result": null, 
      "update": "20120809" 
     }, 
     "AhnLab-V3": { 
      "detected": false, 
      "version": "2012.08.09.00", 
      "result": null, 
      "update": "20120808" 
     }, 
     "VBA32": { 
      "detected": false, 
      "version": "3.12.18.2", 
      "result": null, 
      "update": "20120809" 
     }, 
     "PCTools": { 
      "detected": false, 
      "version": "8.0.0.5", 
      "result": null, 
      "update": "20120809" 
     }, 
     "ESET-NOD32": { 
      "detected": false, 
      "version": "7370", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Rising": { 
      "detected": false, 
      "version": "24.22.02.05", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Ikarus": { 
      "detected": false, 
      "version": "T3.1.1.122.0", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Fortinet": { 
      "detected": false, 
      "version": "4.3.398.0", 
      "result": null, 
      "update": "20120809" 
     }, 
     "AVG": { 
      "detected": false, 
      "version": "10.0.0.1190", 
      "result": null, 
      "update": "20120809" 
     }, 
     "Panda": { 
      "detected": false, 
      "version": "10.0.3.5", 
      "result": null, 
      "update": "20120809" 
     } 
    }, 
    "scan_id": "2aa837802b1c7867a65067525a843566029bd97e3ce99f6eb55217e219043ae1-1344514045", 
    "sha1": "1bebf3ff83636e19cb8e26b6d46472c614aac7ab", 
    "resource": "2aa837802b1c7867a65067525a843566029bd97e3ce99f6eb55217e219043ae1", 
    "response_code": 1, 
    "scan_date": "2012-08-09 12:07:25", 
    "permalink": "https://www.virustotal.com/file/2aa837802b1c7867a65067525a843566029bd97e3ce99f6eb55217e219043ae1/analysis/1344514045/", 
    "verbose_msg": "Scan finished, scan information embedded in this object", 
    "total": 42, 
    "positives": 0, 
    "sha256": "2aa837802b1c7867a65067525a843566029bd97e3ce99f6eb55217e219043ae1", 
    "md5": "6aadd888170411af8aaa0bd8ebffeba4" 
} 
+0

Vì vậy, 'print json_object ['scans']' thực sự xuất ra 'scan_id',' sha1' etc? –

+0

Nếu bạn cho chúng tôi thấy đầu ra của mã của bạn, có thể là đối tượng JSON không phải là cách bạn mong đợi nó. Bạn có thể thêm một json_object in vào mã của bạn và cho chúng ta thấy rằng đầu ra là tốt? – ernie

+1

Tài liệu này khá rõ ràng và tôi nghĩ bạn thực sự có nội dung 'quét', nhưng nó không phải là thứ bạn muốn. Tôi khuyên bạn nên đọc tài liệu Python về cách làm việc với từ điển, danh sách, v.v. –

Trả lời

4

Sử dụng json.loads nó sẽ chuyển đổi các chuỗi json để một dict chứa dicts, danh sách vv

Chỉnh sửa 2: Bạn có thể truy cập vào từng hạng mục như điều này: json_object['scans']['TotalDefense']['version'] tiếp tục. Nhưng bạn cũng có thể cần phải json.loads(json.load(response)) như tôi đã đề cập trong nhận xét của tôi bên dưới.

Chỉnh sửa 1: Trong ví dụ của bạn, chúng tôi sẽ không được xem lại "scans" cho print json_object['scans'].


Từ http://docs.python.org/library/json.html:

>>> import json 
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') 
[u'foo', {u'bar': [u'baz', None, 1.0, 2]}] 

Trang đó cũng có thông tin về làm thế nào để làm giải mã phức tạp hơn.

+0

'.loads' mong đợi một chuỗi, nhưng' urlopen' trả về một đối tượng giống như tệp. Sử dụng '.load' là đúng ở đây. –

+0

Anh ta đã thực hiện 'load'ing yes nhưng tại sao chúng ta lại thấy' "quét" 'khi anh ta in' json_object ['scans'] '? Sau đó anh ta có thể làm 'json.loads (json.load (response))' hoặc 'print json.loads (json_object ['scans'])' để có được một dict anh ta thực sự có thể đi qua. – aneroid

+1

Ah, vì vậy bạn nghĩ rằng 'json_object ['scan']' là một chuỗi chứa JSON ... có lẽ nhưng điều đó có vẻ lạ. Theo tài liệu, nó không: https://www.virustotal.com/documentation/public-api/#getting-file-scans –

2
import json 
import urllib 
import urllib2 
url = "https://www.virustotal.com/vtapi/v2/file/report" 
parameters = {"resource": "2aa837802b1c7867a65067525a843566029bd97e3ce99f6eb55217e219043ae1", 
     "apikey": "12312312312312312"} 
data = urllib.urlencode(parameters) 
req = urllib2.Request(url, data) 
response = urllib2.urlopen(req) 
json_object = response.read() 
print '\n ' 
response_dict = json.loads(json_object) 
print response_dict.get("response_code",{}) 

Kết quả này trả về giá trị của response_code trong đối tượng. Để đi với các giá trị lồng nhau, có thể thực hiện response_dict.get("scans",{}).get("AVG",{}) . CẢM ƠN MỌI NGƯỜI!

Các vấn đề liên quan