0

I'm writing a program using newsapi and wx to get headlines related to certain topics upon demand.

However, the function outputs a block of text like such (if searching for the word 'Tesla'):

{'status': 'ok', 'totalResults': 14, 'articles': [{'source': {'id': 'techcrunch', 'name': 'TechCrunch'}, 'author': 'Darrell Etherington', 'title': "Tesla focuses on service with 25 new service centers in Q2, rate of new openings to 'increase'", 'description': 'Tesla is set to ramp up the rate at which it opens new service facilities aggressively, according to CEO Elon Musk’s guidance on the company’s Q2 2019 earnings call. In total, Tesla opened 25 new service centers during the quarter, and added 100 new service v…', 'url': 'https://techcrunch.com/2019/07/24/tesla-focuses-on-service-with-25-new-service-centers-in-q2-rate-of-new-openings-to-increase/', 'urlToImage': 'https://techcrunch.com/wp-content/uploads/2019/07/GettyImages-1150569888.jpg?w=592', 'publishedAt': '2019-07-24T23:36:47Z', 'content': 'Tesla is set to ramp up the rate at which it opens new service facilities aggressively, according to CEO Elon Musk’s guidance on the company’s Q2 2019 earnings call. In total, Tesla opened 25 new service centers during the quarter, and added 100 new service v… [+2866 chars]'}, {'source': {'id': 'the-verge', 'name': 'The Verge'}, 'author': "Sean O'Kane", 'title': 'Tesla’s longtime CTO is stepping down', 'description': 'Lon.....

I want to get all of the titles to display in a list so I don't have to parse through this massive chunk of text.

My thought is to search for all occurrences of 'title': and then print each phrase that is in the quotes after each occurrence. Any recommendations?

Note: The code that generates this text is:

def NewsSearch(self, event):
    newsapi = NewsApiClient(api_key='8ab524f489d34b278ad537389c789498')
    input = self.tc4.GetValue()
    top_headlines = newsapi.get_top_headlines(q=input)
    print(top_headlines)

where the input is gathered from a TextCtrl panel in wx, and the function is called by binding a button click to the function.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Use the `json` library to parse the response, then you can iterate over the articles and get the title of each one. Don't parse JSON responses from APIs as text when you have wonderful JSON parsing libraries available. – Mihai Chelaru Jul 25 '19 at 01:04
  • @MihaiChelaru the response is already parsed to a dictionary :) – Dani G Jul 25 '19 at 01:05
  • Possible duplicate of [How can I get list of values from dict?](https://stackoverflow.com/questions/16228248/how-can-i-get-list-of-values-from-dict) – Mihai Chelaru Jul 25 '19 at 01:11

2 Answers2

0

The following excerpt is the part you need:

 'articles': [{'source': {'id': 'techcrunch', 'name': 'TechCrunch'}, 'author': 'Darrell Etherington', 'title': "Tesla focuses on service with 25 new service centers in Q2, rate of new openings to 'increase'"

So top_headlines["articles"] is the list of articles being list of dictionaries with single key:value pair of "source": another_dict, and you can get just the titles with:

[article["source"]["title"] for article in top_headlines["articles"]]
ipaleka
  • 3,745
  • 2
  • 13
  • 33
0

Top Headlines is a dictionary, you can extract titles from each article dict and put them into a list.

articles = top_headlines['articles']
titles = [article['title'] for article in articles]
Dani G
  • 1,202
  • 9
  • 16