How to extract information from a python request

Asked

Viewed 39 times

-2

import requests
from requests.structures import CaseInsensitiveDict


  url = f"http://localhost/teste.php"
            headers = CaseInsensitiveDict()
            headers["Accept"] = "*/*"
            resp = requests.get(url, headers=headers)

So I have this simple script, I would like to be able to pick up information that is in the Answer of it

Example: Name":"Joao"

and I wish I could extract only the name Joao Someone could help me?

  • You could provide an example of sponse that can be received?

  • {"Record":{"name":"Joao","email":"[email protected]"}} would just take the name Joao

  • I added an answer, see if it solves your problem.

1 answer

-2


You can use the library json:

import json
import requests

url = "http://localhost/teste.php"
headers = requests.structures.CaseInsensitiveDict()
headers["Accept"] = "*/*"
response = requests.get(url, headers=headers)

# Transforma o texto da resposta em um dicionário
data = json.loads(response.text)

nome = data["Registro"]["nome"]
print(nome)
# Imprime joao

Browser other questions tagged

You are not signed in. Login or sign up in order to post.