Home > Blockchain >  How to make fetch request and get data from object in python?
How to make fetch request and get data from object in python?

Time:03-27

Can someone help me please. I am doing fetch request and trying to get data using python. But I am getting an error.

import requests
import json 

response_API = requests.get('https://newsapi.org/v2/top-headlines?q=sports&country=ru&pageSize=10&apiKey=befce9fd53c04bb695e30568399296c0')
print(response_API.status_code)

data=response_API.text

parse_json=json.loads(response_API)

active_case=parse_json['name']
print('Total results',active_case)

I'm trying to get the name from the following array:

{"status":"ok","totalResults":2,"articles":[{"source":{"id":null,"**name**":"Sports.ru"},"author":"Валерий Левкин","title":"Леброн Джеймс получил «Золотую малину» за худшую актерскую работу - Sports.ru","description":"В США названы обладатели антинаграды «Золотая малина» по итогам 2021 года.","url":"https://www.sports.ru/basketball/1107870293-lebron-dzhejms-poluchil-zolotuyu-malinu-za-xudshuyu-akterskuyu-rabotu.html","urlToImage":"https://www.sports.ru/dynamic_images/news/110/787/029/3/share/bd571e.jpg","publishedAt":"2022-03-26T13:03:00Z","content":null}]}

Got error, value is not returned.

CodePudding user response:

The newsapi URL returns JSON content with a list of articles, where each article has this structure:

{
    "source": {
        "id": null,
        "name": "Sports.ru"
    },
    "author": "...",
    "title": "... - Sports.ru",
    "description": "...",
    "url": "https://www.sports.ru/basketball/1107870293-lebron-dzhejms-poluchil-zolotuyu-malinu-za-xudshuyu-akterskuyu-rabotu.html",
    "urlToImage": "https://www.sports.ru/dynamic_images/news/110/787/029/3/share/bd571e.jpg",
    "publishedAt": "2022-03-26T13:03:00Z",
    "content": null
}

To extract a particular element such as description from each article then try this:

import requests
import json 

response = requests.get('https://newsapi.org/v2/top-headlines?q=sports&country=ru&pageSize=10&apiKey=befce9fd53c04bb695e30568399296c0')
print(response.status_code)

response.encoding = "utf-8"
data = response.json()

# to get the name from source of each article
print([article["source"].get("name") for article in data["articles"]])

# to get the descriptions from each article
# where desc will be a list of descriptions
desc = [article["description"] for article in data["articles"]]
print(desc)

Output:

200
['Sports.ru', 'Sports.ru']
['description1', 'description2']

CodePudding user response:

You need to follow the nesting of objects:

  1. First get the key 'articles'
  2. Then get the first element of the list
  3. Then get the key 'source'
  4. Finally get the key 'name'.

You can do this all in a single line with indexes.

CodePudding user response:

slightly different method, but same result using your original technique as the basis. You get a json string, then convert that to json, then search for the bit that you want.

import requests
import json 

response_API = requests.get('https://newsapi.org/v2/top-headlines?q=sports&country=ru&pageSize=10&apiKey=befce9fd53c04bb695e30568399296c0')
print(response_API.status_code)

# this is a json string
data=response_API.text

# convert string to json
parse_json=json.loads(data)

print('here is the json....')
print(parse_json)

# get an element form json
active_case=parse_json['articles'][0]

# print the result
print('here is the active case...')
print(active_case)

This is the result, from which you can extract what you like from it:

{'source': {'id': None, 'name': 'Sports.ru'}, 'author': 'Валерий Левкин', 'title': 'Леброн Джеймс получил «Золотую малину» за худшую актерскую работу - Sports.ru', 'description': 'В США названы обладатели антинаграды «Золотая малина» по итогам 2021 года.', 'url': 'https://www.sports.ru/basketball/1107870293-lebron-dzhejms-poluchil-zolotuyu-malinu-za-xudshuyu-akterskuyu-rabotu.html', 'urlToImage': 'https://www.sports.ru/dynamic_images/news/110/787/029/3/share/bd571e.jpg', 'publishedAt': '2022-03-26T13:03:00Z', 'content': None}, {'source': {'id': None, 'name': 'Sports.ru'}, 'author': 'Андрей Карнаухов', 'title': 'Овечкин забил 771-й гол в НХЛ. До Хоу – 30 шайб - Sports.ru', 'description': 'Капитан\xa0«Вашингтона»\xa0Александр Овечкин\xa0забросил\xa0шайбу, а также забил победный буллит в серии в матче с «Баффало» (4:3 Б) и был признан третьей звездой.', 'url': 'https://www.sports.ru/hockey/1107860736-ovechkin-zabil-771-j-gol-v-nxl-do-xou-30-shajb.html', 'urlToImage': 'https://www.sports.ru/dynamic_images/news/110/786/073/6/share/c9cb18.jpg', 'publishedAt': '2022-03-26T01:56:15Z', 'content': None}

Here the result is a simple dict.

  • Related