Python Forum
'NoneType' object is not subscriptable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'NoneType' object is not subscriptable
#1
I'm writing an API for a Django application that gets inventory from another website. The code was working at first but recently I get the error "NoneType object is not Subscriptable". I'm new to this but I understand why NoneType is not Subscriptable. I just can't figure out how to resolve it in my code.




headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3)'
                  ' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}

login_url = 'http://www.website.com/login/validate_login'
inventory_url = 'http://www.website.com:1002/orders/t_lookup'
t_search_url = 'http://www.website.com:1002/orders/do_t_lookup?update=t_list'
t_fitment_url = 'http://www.website.com:1002/fit/update_fit'

login_params = {
    "commit": "Login",
    "login[account]": os.getenv("tsearch_number"),
    "login[password]": os.getenv("tsearch_number"),
    "login[sales]": "1400"
}

t_search_params = {
    "authenticity_token": None,
    "commit": "Find",
    "search_options[exact]": 1,
    "search_options[max]": 300,
    "search_options[scol]": 1,
    "search_options[show_cost]":
        {0: 0,
         1: 1},
    "search_options[show_retail]": 0,
    "search_options[size]": None,
    "search_options[zero]": 0,
    "utf8": True
}



cookie_dir = "extras/"
cookie_file = cookie_dir + "session.cookie"

logged_in = False
session = requests.Session()
session.headers = headers
if os.path.exists(cookie_file):
    print("Found existing cookies. Loading into session.")
    with open(cookie_file, "rb") as f:
        cookies = requests.utils.cookiejar_from_dict(pickle.load(f))
    session.cookies.update(cookies)
    logged_in = True

if not os.path.exists(cookie_dir):
    print("Extras folder not found. Creating.")
    os.mkdir(cookie_dir)

if not logged_in:
    print("Not signed into website. Signing in.")
    session.post(login_url, data=login_params, headers=headers)
    with open(cookie_file, "wb") as f:
        pickle.dump(session.cookies.get_dict(), f)

s = session.get(inventory_url)

b = bs4(s.text, "html5lib")
auth = b.find(attrs={"name": "authenticity_token"})["Value"]
token = t_search_params["authenticity_token"]
if "login" in s.url:
    GlobalSettings.search_enabled = False
    raise Exception("Login failed")
else:
    GlobalSettings.search_enabled = True
This is the error I get
'NoneType' object is not subscriptable
Request Method: GET
Request URL:    https://testsite.com/
Django Version: 2.2.6
Exception Type: TypeError
Exception Value:    
'NoneType' object is not subscriptable
Exception Location: /home/website/projectdir/website/t_search.py in <module>, line 68
Python Executable:  /home/testsite/projectdir/djangoprojectenv/bin/python
Python Version: 3.6.9
Python Path:    
['/home/testsite/projectdir',
 '/home/testsite/projectdir/djangoprojectenv/bin',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/home/testsite/projectdir/djangoprojectenv/lib/python3.6/site-packages']
Server time:    Sat, 1 Aug 2020 03:55:21 +0000
Traceback, any help would be greatly appreciated
File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  100.         resolver_match = resolver.resolve(request.path_info)

File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/urls/resolvers.py" in resolve
  536.             for pattern in self.url_patterns:

File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/utils/functional.py" in __get__
  80.         res = instance.__dict__[self.name] = self.func(instance)

File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/urls/resolvers.py" in url_patterns
  584.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/utils/functional.py" in __get__
  80.         res = instance.__dict__[self.name] = self.func(instance)

File "/home/testsite/djangoprojectdir/djangoprojectenv/lib/python3.6/site-packages/django/urls/resolvers.py" in urlconf_module
  577.                 return import_module(self.urlconf_name)

File "/usr/lib/python3.6/importlib/__init__.py" in import_module
  126.     return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>" in _gcd_import
  994. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load
  971. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
  955. <source code not available>

File "<frozen importlib._bootstrap>" in _load_unlocked
  665. <source code not available>

File "<frozen importlib._bootstrap_external>" in exec_module
  678. <source code not available>

File "<frozen importlib._bootstrap>" in _call_with_frames_removed
  219. <source code not available>

File "/home/testsite/projectdir/testsites/urls.py" in <module>
  22.     url(r'^', include('website.urls'), name='website'),

File "/home/testsite/projectdir/djangoprojectenv/lib/python3.6/site-packages/django/urls/conf.py" in include
  34.         urlconf_module = import_module(urlconf_module)

File "/usr/lib/python3.6/importlib/__init__.py" in import_module
  126.     return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>" in _gcd_import
  994. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load
  971. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
  955. <source code not available>

File "<frozen importlib._bootstrap>" in _load_unlocked
  665. <source code not available>

File "<frozen importlib._bootstrap_external>" in exec_module
  678. <source code not available>

File "<frozen importlib._bootstrap>" in _call_with_frames_removed
  219. <source code not available>

File "/home/testsite/djangoprojectdir/website/urls.py" in <module>
  7. from . import views

File "/home/testsite/djangoprojectdir/website/views.py" in <module>
  9. import website.tire_search as ts

File "/home/testsite/djangoprojectdir/website/t_search.py" in <module>
  68. auth = b.find(attrs={"name": "authenticity_token"})["value"]

Exception Type: TypeError at /
Exception Value: 'NoneType' object is not subscriptable
Reply
#2
Line 68 (which is missing in your post): bs.find() returns None if nothing were found.
The square brackets after bs.find() is the subscription.
First check the return value of bs.find() and if it is not None, then access the value.
If None was returned, then skip it.

Maybe the class attribute has a different name and/or the html-code has been changed.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
How can I find out if the class attribute or HTML has changed? I've combed google with no good results.
Reply
#4
You can run for example the Python repl (or better IPython) with your program in interactive mode.

python3 -i your_program.py
Then the exception will appear and then you can use the b or bs object.

Open your browser, press F12 and select the html element you want to inspect.
Then you'll see the class names. Then try in the python repl to find the element.


Sometimes you get different results with requests and a real browser,
because some pages are manipulating the DOM with JavaScript.

In this case you could try: https://pypi.org/project/requests-html/
A step further is selenium, which controls a headless remote browser (Firefox, Chrome).
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
The same code was working perfectly until I modified my admin.py, so it seems the issue may not have anything to do with the line of code shown. I still get the error after trying the options suggested, I never used selenium but I may give it a try.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 737 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 985 Aug-24-2023, 05:14 PM
Last Post: snippsat
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,460 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Help with python 'not subscriptable' error Extra 3 2,083 Dec-16-2022, 05:55 PM
Last Post: woooee
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,239 Sep-19-2022, 02:43 PM
Last Post: Larz60+
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 4,682 Jan-02-2022, 09:53 AM
Last Post: snippsat
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 2,670 Jan-01-2022, 04:16 PM
Last Post: MaartenRo
  'int' object is not subscriptable after API call ed8484 1 1,805 Sep-18-2021, 02:06 PM
Last Post: ed8484
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 5,238 Aug-17-2021, 05:24 PM
Last Post: Franky77
  Message='int' object is not subscriptable DanielCook 4 2,305 Aug-10-2021, 05:21 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020