簡體   English   中英

如何從 JSON 響應中提取特定文本

[英]How to extract specific text from JSON response

我正在使用 Twitter API 來獲取推文,並使用他們提供的 GitHub 示例。 但是,推文以 JSON 格式給出。 如何提取一部分(特別是文本和標簽)並將其放入列表 dataframe(最理想的選項)、dict 等中?

    for response_line in response.iter_lines():
    if response_line:
        json_response = json.loads(response_line)
        tweets.append(json_response)

查看“推文”列表后,數據使用以下格式:

{'data': {'id': '1562362833374945281',
'text': 'Waiting 😃......'},
'matching_rules': [{'id': '1562362617708027906', 'tag': 'bitcoin'}]}

json.loads()返回一個字典。 所以例如獲取文本:

json_response = json.loads(response_line)
text = json_response.get("data").get("text")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM