2
 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.

6
  • 1
    @LucaCappelletti I think newspapers is a module. newspaper.readthedocs.io/en/latest Commented Jul 7, 2018 at 9:24
  • what do you mean? Commented Jul 7, 2018 at 9:24
  • What do you intend to do while writing that? Commented Jul 7, 2018 at 9:25
  • im going to use it as part of another project im working on Commented Jul 7, 2018 at 9:27
  • and my code is just like the docs Commented Jul 7, 2018 at 9:27

1 Answer 1

9

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much , now i understand
Thanks for a good explanation for a simple but perplexing error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.