Type Object Not Subscriptable - Python
from newsapi.sources import Sources
import json
api_key ='*******************'
s = Sources(API_KEY=api_key)
they input the category of news they want
wanted = input('> ')
source_list = s.get(category=wanted, language='en')
index = 0
sources = []
getting the sources for source in source_list["sources"]:
data = json.dumps(source_list)
data = json.loads(data)
source = (data["sources"][index]["url"])
sources.append(source)
index += 1
from newspaper import Article
i = len(sources) - 1
looping through the source list and printing the articles for source in sources:
url_ = sources[i]
a = Article[url_]
print(a)
i -= 1
getting error 'type' object is not subscriptable on the line a = Article[url_]
have researched but still do not understand why in my case.
Answer
The simple solution to your problem is that the line:
a = Article[url_]
Should be:
a = Article(url_)
Now to get to why you're getting the TypeError: 'type' object is not subscriptable
error.
This TypeError
is the one thrown by python when you use the square bracket notation object[key]
where an object doesn't define the __getitem__
method. So for instance, using []
on an object
throws:
>>> object()["foo"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'object' object is not subscriptable
In this case []
s were used accidentally instead of ()
s when trying to instantiate a class. Most classes (including this Article
class) are instances of the type
class, so trying object["foo"]
causes the same error you are experiencing:
>>> object["foo"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module