JSON 请求从字典中获取特定值 Python
JSON requests get specific value from dict Python
我正在尝试使用来自 YouTube 的这段代码来检测 Discord 聊天中的特定用户名。如果用户名是,请说“123”,然后将其打印为 "Found: 123"。尝试了各种方法都没有用。
这是我的完整代码:
import websocket
import json
import threading
import time
def send_json_request(ws, request):
ws.send(json.dumps(request))
def receive_json_response(ws):
response = ws.recv()
if response:
return json.loads(response)
def heartbeat(interval, ws):
print("Heartbeat begin")
while True:
time.sleep(interval)
heartbeatJSON = {
"op": 1,
"d": "null"
}
send_json_request(ws, heartbeatJSON)
print("Heartbeat sent")
ws = websocket.WebSocket()
ws.connect('wss://gateway.discord.gg/?v=6&encording=json')
event = receive_json_response(ws)
heartbeat_interval = event['d']['heartbeat_interval'] / 1000
threading._start_new_thread(heartbeat, (heartbeat_interval, ws))
token = "tokenhere"
payload = {
'op': 2,
"d": {
"token": token,
"properties": {
"$os": "windows",
"$browser": "chrome",
"$device": "pc"
}
}
}
send_json_request(ws, payload)
while True:
event = receive_json_response(ws)
try:
print(f"{event['d']['author']['username']}: {event['d']['content']}")
print(event)
op_code = event('op')
for specific in event:
if specific['d']['author']['username'] == "123":
print(f'Found: {specific}')
else:
print("Not found")
if op_code == 11:
print("Heartbeat received")
except:
pass
无效的代码(但没有错误):
for specific in event:
if specific['d']['author']['username'] == "123":
print(f'Found: {specific}')
else:
print("Not found")
好像哪里不对?谢谢。
我不应该使用 for 循环。我尝试了下面的代码,它起作用了。这是一个如此简单的错误:
specific = event['d']['author']['username']
if specific == "123":
print(f"Found")
else:
print("Not found")
我正在尝试使用来自 YouTube 的这段代码来检测 Discord 聊天中的特定用户名。如果用户名是,请说“123”,然后将其打印为 "Found: 123"。尝试了各种方法都没有用。
这是我的完整代码:
import websocket
import json
import threading
import time
def send_json_request(ws, request):
ws.send(json.dumps(request))
def receive_json_response(ws):
response = ws.recv()
if response:
return json.loads(response)
def heartbeat(interval, ws):
print("Heartbeat begin")
while True:
time.sleep(interval)
heartbeatJSON = {
"op": 1,
"d": "null"
}
send_json_request(ws, heartbeatJSON)
print("Heartbeat sent")
ws = websocket.WebSocket()
ws.connect('wss://gateway.discord.gg/?v=6&encording=json')
event = receive_json_response(ws)
heartbeat_interval = event['d']['heartbeat_interval'] / 1000
threading._start_new_thread(heartbeat, (heartbeat_interval, ws))
token = "tokenhere"
payload = {
'op': 2,
"d": {
"token": token,
"properties": {
"$os": "windows",
"$browser": "chrome",
"$device": "pc"
}
}
}
send_json_request(ws, payload)
while True:
event = receive_json_response(ws)
try:
print(f"{event['d']['author']['username']}: {event['d']['content']}")
print(event)
op_code = event('op')
for specific in event:
if specific['d']['author']['username'] == "123":
print(f'Found: {specific}')
else:
print("Not found")
if op_code == 11:
print("Heartbeat received")
except:
pass
无效的代码(但没有错误):
for specific in event:
if specific['d']['author']['username'] == "123":
print(f'Found: {specific}')
else:
print("Not found")
好像哪里不对?谢谢。
我不应该使用 for 循环。我尝试了下面的代码,它起作用了。这是一个如此简单的错误:
specific = event['d']['author']['username']
if specific == "123":
print(f"Found")
else:
print("Not found")