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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With