Python键错误:“destinationAccount”
我从一个正在抓取数据的网站上用以下结构编写了代码: 当我试着用这个读这把钥匙的时候Python键错误:“destinationAccount”,python,web-scraping,python-requests,Python,Web Scraping,Python Requests,我从一个正在抓取数据的网站上用以下结构编写了代码: 当我试着用这个读这把钥匙的时候 vban = str(transaction["destinationAccount"]["vban"]) 它给了我一个关键错误:“destinationAccount” 有人知道为什么会出现这种情况吗?当我运行代码时,它会将我需要的所有内容复制到MySQL数据库中,但正如我前面所说的,KeyError弹出窗口和间隔不起作用 sched = BlockingSchedul
vban = str(transaction["destinationAccount"]["vban"])
它给了我一个关键错误:“destinationAccount”
有人知道为什么会出现这种情况吗?当我运行代码时,它会将我需要的所有内容复制到MySQL数据库中,但正如我前面所说的,KeyError弹出窗口和间隔不起作用
sched = BlockingScheduler()
sched.add_job(start, 'interval', seconds=5)
sched.start()
因为它在错误出现后停止运行。当我注释掉这个vban=strtransaction[destinationAccount][vban]时,没有出现错误。我现在检查了10多次,网站的结构如我在顶部所示。任何解决方案都将是惊人的
def getData():
databaseConn = dbConnect()
cursor = databaseConn.cursor()
for x in range(3):
x = x * 25
transactions = json.loads(makeRequest("URL.bla/transactions?offset=" + str(x), authToken, True).text)
for transaction in transactions:
person = ""
try:
person = transaction["destinationAccount"]["ownerCharacter"]["name"]
except:
try:
person = transaction["destinationAccount"]["ownerFactory"]["label"]
except:
try:
person = transaction["destinationAccount"]["ownerBuilding"]["label"]
except:
person = str("unbekannt")
reference = ""
try:
reference = str(translateTable[transaction["reference"]])
except:
reference = str(transaction["reference"])
vban = str(transaction["destinationAccount"]["vban"])
amount = str(transaction["amount"])
taxAmount =str(transaction["taxAmount"])
gesamt = (float(amount) + float(taxAmount))
created = parse(str(transaction["created"]))
date = str(created.date())
time = str(created.time()).split(".")[0]
sql = "INSERT INTO finanzen (transaktion, date, time, sendto, vban, amount, tax, gesamt, text) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = (str(transaction["uuid"]), date, time, str(person), vban, amount, taxAmount, gesamt, reference)
try:
cursor.execute(sql, val)
databaseConn.commit()
except:
print("Fehler Datenbank")
dbClose(databaseConn,cursor)
打印结果:
{'_id': 'CENSORED',
'uuid': 'CENSORED',
'amount': 11.8421,
'taxAmount': 3.1479,
'type': 'digital',
'created': 'Date',
'reference': 'CENSORED',
'sourceAccount': {'_id': 'CENSORED',
'ownerCharacter': {'_id': 'CENSORED',
'name': 'NAME'},
'vban': 'NUMBER'},
'destinationAccount': {'_id': 'CENSORED',
'vban': 'NUMBER',
'ownerBuilding': {'_id': 'CENSORED',
'label': 'Eclipse Towers'}}}
看不到完整的列表很难,但我怀疑有些项目缺少关键。您是否尝试过检查现有的密钥。以您的例子:
transaction = {
"_id":"CENSORED",
"uuid":"CENSORED",
"amount":11.8421,
"taxAmount":3.1479,
"type":"digital",
"created":"Date",
"reference":"CENSORED",
"sourceAccount":{
"_id":"CENSORED",
"ownerCharacter":{
"_id":"CENSORED",
"name":"NAME"
},
"vban":"NUMBER"
},
"destinationAccount":{
"_id":"CENSORED",
"ownerBuilding":{
"_id":"CENSORED",
"label":"Eclipse Towers"
}
}
}
if 'vban' in transaction['destinationAccount']:
vban = str(transaction["destinationAccount"]["vban"])
else:
vban = "none"
感谢@Johnny John Boy的提示
vban = ""
try:
vban = str(transaction["destinationAccount"]["vban"])
except:
try:
vban = str(transaction["sourceAccount"]["vban"])
except:
vban = str("Unbekannt")
这是修复KeyError的解决方案,因为还有第二部分。现在它可以正常工作了,没有任何错误。请将您运行的python代码粘贴到main post。请在transactions=json.loadsmakeRequestURL.bla/transactions?offset=+strx、authToken、True.text和update questionUpdated之后打印事务。但正如我所说的,我已经测试过了,但根本没有发现任何错误。什么是[在打印的开始?这是一个列表吗?在哪里]?密钥肯定存在。在我问题的顶部,我还写道,当我执行所有工作时,这个关键错误无论如何都会出现。所有内容都按预期复制到数据库中。总有一个号码,没有不可用号码的条目。因为它们是传输。顺便说一句,你给出的解决方案给出了相同的错误,而且完整的列表在第一篇文章中,它只是不断重复与其他数字,名称。
vban = ""
try:
vban = str(transaction["destinationAccount"]["vban"])
except:
try:
vban = str(transaction["sourceAccount"]["vban"])
except:
vban = str("Unbekannt")