简体   繁体   English

AttributeError:“ NoneType”对象没有属性“ group” {for instagram}

[英]AttributeError: 'NoneType' object has no attribute ‘group’ {for instagram}

Lokesh-mac-mini-pro-2:instabot.py-master Lokeshmacminipro$ python example.py
02.09.2018_10:03 Instabot v1.2.0 started at 02.09.2018 10:03:

02.09.2018_10:03 Trying to login as moneshrallapalli...
Traceback (most recent call last):
  File "example.py", line 52, in <module>
    unfollow_whitelist=['example_user_1', 'example_user_2'])
  File "/Users/lokemone/Desktop/instabot.py-master/src/instabot.py", line 234, in __init__
    self.login()
  File "/Users/lokemone/Desktop/instabot.py-master/src/instabot.py", line 303, in login
    self.user_id = ui.get_user_id_by_login(self.user_login)
  File "/Users/lokemone/Desktop/instabot.py-master/src/userinfo.py", line 42, in get_user_id_by_login
    json_info = json.loads(re.search('{"activity.+show_app', info.text, re.DOTALL).group(0)+'":""}')
AttributeError: 'NoneType' object has no attribute 'group'

Lokesh-mac-mini-pro-2:instabot.py-master Lokeshmacminipro$ 

please tell how to avoid 请告诉我如何避免

AttributeError: 'NoneType' object has no attribute 'group' AttributeError:'NoneType'对象没有属性'group'

This error means the value does not contain anything ie It has None values. 此错误表示该值不包含任何内容,即它没有值。 Check it before grouping. 分组之前检查它。

It is evident from the traceback that the below line is responsible for the exception. 从回溯中可以明显看出,下一行是造成异常的原因。

json_info = json.loads(re.search('{"activity.+show_app', info.text, re.DOTALL).group(0)+'":""}')

It looks like you are trying to fetch some specific things from the info.text and then make a json with it. 看来您正在尝试从info.text获取一些特定的info.text ,然后使用它制作一个json。 In that case, your approach needs a little modification. 在这种情况下,您的方法需要进行一些修改。

Let's first fix the search part. 让我们首先修复搜索部分。 It should be, 它应该是,

matched_string = re.search('activity.+show_app', info.text, re.DOTALL).group(0)

The specific Exception you are getting is because of this line. 您收到的特定异常是由于此行。 The pattern you were searching was not in that string. 您正在搜索的模式不在该字符串中。 So, you can just add a try/catch or if/else there to avoid that scenario. 因此,您可以在其中添加一个try / catch或if / else来避免这种情况。

Then make it a valid json explicitly, by doing: 然后通过执行以下操作将其显式设置为有效的json:

 valid_json = "{" + matched_string + ": ''}"

Finally, load it with json module. 最后,使用json模块加载它。

 valid_dict = json.loads(valid_json)

So, in one line, you can do, 因此,只需一行,您就可以做到,

try:
    some_var = json.loads("{" + re.search('activity.+show_app', info.text, re.DOTALL).group(0) + ": '' }")
except AttributeError:
    print("Couldn't find that specific pattern")

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM