首页 > Python:NameError: name 'null' is not defined

Python:NameError: name 'null' is not defined

问题代码

html ="""["http:\/\/weibo.com\/2084584685",10,null,null,false,true,0]"""
html = eval(html)

报错

NameError: name 'null' is not defined

html是采集后从源码中获取的,除了给null,false,true添加双引号以外,还有其他解决办法吗?


Python的null叫做None。但这不光是null的问题!

这个原来应该是js代码,虽然不完全一致,不过很大程度上,是可以用JSON库去解的。

且不说eval的安全性问题。根本不是Python代码的东西,拿Python解析岂不是毫无道理?

>>> html ="""["http:\/\/weibo.com\/2084584685",10,null,null,false,true,0]"""
>>> import json
>>> obj = json.loads(html)
>>> obj
[u'http://weibo.com/2084584685', 10, None, None, False, True, 0]
【热门文章】
【热门文章】