Django:AttributeError:' NoneType'对象没有属性' group'错误

时间:2015-06-02 01:45:03

标签: python regex django attributes regex-group

我想要正确的代码

try:
        res = self.browser.open('https://login.facebook.com/login.php?login_attempt=1',form_data)
        response = res.read()
        self.fbid = re.search('https://www.facebook.com/(.*)\?sk=info',response)
        self.fbid = re.search('https://www.facebook.com/(.*)',self.fbid.group(1))
        self.fbid = re.search('https://www.facebook.com/(.*)',self.fbid.group(1))
    except urllib2.HTTPError,e :
        print "****exception****inside login  error code: %s" % (e.code)
    res.close()
except urllib2.HTTPError,e :
     print "****exception****inside login  error code: %s" % (e.code)

#print " server Response Code: %s " % (res.code)

我收到了这个错误

File "facebotv2.py", line 122, in login
self.fbid = re.search('https://www.facebook.com/(.*)',self.fbid.group(1))
AttributeError: 'NoneType' object has no attribute 'group'

此处的完整代码:https://github.com/pun1sh3r/facebot/blob/master/facebotv2.py

1 个答案:

答案 0 :(得分:0)

我最好的猜测是,您的response与您的正则表达式'https://www.facebook.com/(.*)\?sk=info'不匹配,这就是为什么它没有捕获任何组

始终检查您的re.search是否返回对象

if self.fbid:
      self.fbid = re.search('https://www.facebook.com/(.*)',self.fbid.group(1))
else:
    """do something else"""

如果你的正则表达式匹配某些东西,它可能会显示类似<_sre.SRE_Match object at 0xb7166410>的内容,这会使你的if条件为真,你可以使用分组从该匹配中提取组