我对我的Python代码有疑问。
我有这个代码试图通过制作一个json文件来查找网页上的更改并进行比较以查看是否每5秒钟有任何更改但我收到此错误。我认为代码中的其他所有内容都应该有效。我该如何解决这个问题?
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\binance-getmarketsalert-master\test.py", line 28, in <module>
before_set = set([market['symbol'] for market in before['result']])
TypeError: list indices must be integers or slices, not str
代码是这样的:
import time
from smtplib import SMTPException
def build_pair_string(pairs):
pair_string = ""
for pair in pairs:
pair_string += pair + "\n"
return pair_string
while True:
try:
with open('markets.json', 'r') as current_markets:
before = json.loads(current_markets.read())
except IOError:
before = requests.get('').json()
with open('markets.json', 'w') as current_markets:
current_markets.write(json.dumps(before))
print("First run... Getting initial market data.")
after = requests.get('').json()
new_set = after_set - before_set
if not new_set:
print('No changes.. Waiting 5 seconds')
time.sleep(5)
if new_set:
with open('markets.json', 'w') as current_markets:
duration = 500 # millisecond
freq = 440 # Hz
winsound.Beep(freq, duration)
print('Binance has added the following pairs:')
new_list = []
for pair in new_set:
new_list.append(pair) # = [item for item in pair]
print(new_list)
sender = 'sender@gmail.com'
receivers = ['receiver@gmail.com']
message = """
Binance added {}
""".format(build_pair_string(new_list))
try:
smtpObj = smtplib.SMTP_SSL('smtp.gmail.com:465')
smtpObj.login('example@gmail.com','password')
print("\nSuccessfully sent email")
except SMTPException as e:
print(e)
print("\nError: unable to send email")
logging.critical('Waiting 5 seconds. New coin was listed')
time.sleep(5)