最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Python调用印象笔记noteStore.updateNote报错:EDAMUserException errorCode 11 parameter The element type en-note must be terminated by the matching end-tag en-note

报错 crifan 400浏览 0评论
折腾:
【未解决】Python处理发布印象笔记帖子到WordPress后的部分细节优化
期间,调试把一个帖子发布到WordPress期间
遇到代码:
libs/crifan/crifanEvernote.py
    def syncNote(self,
            noteGuid,
            noteTitle,
            notebookGuid=None,
            newContent=None,
            newResList=None,
            newAttributes=None,
        ):
。。。
    updatedNote = self.noteStore.updateNote(newNote)
报错:
发生异常: EDAMUserException
EDAMUserException(errorCode=11, parameter='The element type "en-note" must be terminated by the matching end-tag "</en-note>".')
  File "/Users/crifan/dev/dev_root/python/EvernoteToWordpress/EvernoteToWordpress/libs/evernote-sdk-python3/lib/evernote/edam/notestore/NoteStore.py", line 4798, in recv_updateNote
    raise result.userException
  File "/Users/crifan/dev/dev_root/python/EvernoteToWordpress/EvernoteToWordpress/libs/evernote-sdk-python3/lib/evernote/edam/notestore/NoteStore.py", line 4774, in updateNote
    return self.recv_updateNote()
  File "/Users/crifan/dev/dev_root/python/EvernoteToWordpress/EvernoteToWordpress/libs/evernote-sdk-python3/lib/evernote/api/client.py", line 167, in delegate_method
    )(**dict(list(zip(arg_names, args))))
  File "/Users/crifan/dev/dev_root/python/EvernoteToWordpress/EvernoteToWordpress/libs/crifan/crifanEvernote.py", line 230, in syncNote
    updatedNote = self.noteStore.updateNote(newNote)
  File "/Users/crifan/dev/dev_root/python/EvernoteToWordpress/EvernoteToWordpress/EvernoteToWordpress.py", line 582, in processAndSyncNote
    respNote = gEvernote.syncNote(**syncParamDict)
  File "/Users/crifan/dev/dev_root/python/EvernoteToWordpress/EvernoteToWordpress/EvernoteToWordpress.py", line 501, in processNoteCallback
    processAndSyncNote(curNote)
  File "/Users/crifan/dev/dev_root/python/EvernoteToWordpress/EvernoteToWordpress/EvernoteToWordpress.py", line 1096, in processSingleNotebook
    processCallback(eachFoundNote)
  File "/Users/crifan/dev/dev_root/python/EvernoteToWordpress/EvernoteToWordpress/EvernoteToWordpress.py", line 1111, in EvernoteToWordpress
    processSingleNotebook(toProcessNotebookGuid, processNoteCallback)
  File "/Users/crifan/dev/dev_root/python/EvernoteToWordpress/EvernoteToWordpress/EvernoteToWordpress.py", line 1190, in <module>
    EvernoteToWordpress()
问题现象很明显:
EDAMUserException(errorCode=11, parameter='The element type "en-note" must be terminated by the matching end-tag "</en-note>".')
本来以为是之前的en-media的tag呢
现象发现看错了,此处指的是:
en-note
最顶级的节点
所以去看看当前的html是什么
最底部:
很明显,多出一个 </html>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note>
。。。
  </html>
</en-note>
所以要去找找,之前是哪个地方的代码处理content到html,互相转换期间,出错了。
发现是:
def processAndSyncNote(curNote):
。。。
    # process title+link
    noteDetail = mergeNoteTitleAndUrl(noteDetail)
之后,就多了 </html>
去看看代码
感觉像是之前的re.sub的正则,写的不够好
调试发现了,果然是:
noteContent = re.sub('(<html>)?(?P<contentBody>.+)(</html>)?', "<en-note>\g<contentBody></en-note>", noteHtml, flags=re.S)
导致的,多出了 </html>
所以去写代码:
        # support both xxx and <html>xxx</html>
        pureHtmlBody = re.sub('<html>(?P<contentBody>.+)</html>', "\g<contentBody>", noteHtml, flags=re.S)
        noteContent = "<en-note>%s</en-note>" % pureHtmlBody
调试看看
就正常了。不会多出额外的 </html> 了。
然后即可解决问题了。
【总结】
此处处理印象笔记中的content,即html,之前不小心re.sub的规则写的有问题:
noteContent = re.sub('(<html>)?(?P<contentBody>.+)(</html>)?', "<en-note>\g<contentBody></en-note>", noteHtml, flags=re.S)
导致content中多出一个无效的 </html>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note>
。。。
  </html>
</en-note>
导致去
self.noteStore.updateNote
时报错:
发生异常: EDAMUserException
EDAMUserException(errorCode=11, parameter='The element type "en-note" must be terminated by the matching end-tag "</en-note>".')
解决办法:
经调试后修正为:
        # support both xxx and <html>xxx</html>
        pureHtmlBody = re.sub('<html>(?P<contentBody>.+)</html>', "\g<contentBody>", noteHtml, flags=re.S)
        noteContent = "<en-note>%s</en-note>" % pureHtmlBody
即可保证content是正确的:
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note>
。。。
</en-note>
而解决问题,不会报错了。

转载请注明:在路上 » 【已解决】Python调用印象笔记noteStore.updateNote报错:EDAMUserException errorCode 11 parameter The element type en-note must be terminated by the matching end-tag en-note

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
97 queries in 0.212 seconds, using 23.32MB memory