首页 > 解决方案 > 分析一段文本并在关键字之后打印短语

问题描述

我正在编写一个程序,使用 newsapi 和 wx 根据需要获取与某些主题相关的标题。

但是,该函数会输出这样的文本块(如果搜索单词“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.....

我想让所有的标题显示在一个列表中,这样我就不必解析大量的文本。

我的想法是搜索所有出现的“标题”:然后在每次出现后打印引号中的每个短语。有什么建议吗?

注意:生成此文本的代码是:

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

其中输入是从 wx 中的 TextCtrl 面板收集的,并且通过将按钮单击绑定到该函数来调用该函数。

标签: python

解决方案


以下摘录是您需要的部分:

 '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'"

top_headlines["articles"]文章列表也是具有单个 key:value 对的字典列表,"source": another_dict您可以仅获取标题:

[article["source"]["title"] for article in top_headlines["articles"]]

推荐阅读