0
response = requests.request("GET", url, headers=headers)
print(response.text)
pdfLinks = []

I use an api to extract such a result, which is very long.This response.text seems to be a string, not a dictionary.

{"results":[{"title":"montrac® INTELLIGENCE IN MOTION - montratec","link":"https://www.montratec.de/fileadmin/user_upload/footer/montratec_Design_Guide_ENG.pdf","description":"„With montratec's technology, we could customize the transport system according to our production needs, eliminating dust, reducing consumption and optimizing. Missing: filetype= filetype=",.....},

For each item in the list, I want to extract only its "link" element and put it inside a new list. How can I achieve this?

x89
  • 2,798
  • 5
  • 46
  • 110
  • 3
    If the API server is sending the response in json format, then you can use the `response.json()` to transform the api response in python dictionary. – Shubham Sharma Jul 15 '21 at 13:26
  • Is the answer I gave below working for you? If so it would be great if you could mark as the answer, otherwise let us know if you have an issue still. – SimonT Jul 15 '21 at 14:15

1 Answers1

2

Updated to match your question update.

You could just use data = response.json() to get the information back straight into a dictionary since you are using requests module.

If you want to continue as a string (not recommended) and not a dict you can run literal_eval (from ast library) on it.

Then using dict.get and list comprehension you can get what you want.

.get will return the value of key 'results', if it doesn't exist then it will return None and not throw any KeyErrors.
List comprehension creates a new list from the results of what is essentially a for loop inside that iterates through the contents of results.

with requests.request("GET", url, headers=headers) as resp:
    data = resp.json()

foo = [i['link'] for i in data.get('results')]

If you want to keep it as a string for some reason

from ast import literal_eval

a = str({"results":[{"title":"montrac® INTELLIGENCE IN MOTION - montratec","link":"https://www.montratec.de/fileadmin/user_upload/footer/montratec_Design_Guide_ENG.pdf","description":"„With montratec's technology, we could customize the transport system according to our production needs, eliminating dust, reducing consumption and optimizing. Missing: filetype= filetype="}]})

b = literal_eval(a)

c = [i['link'] for i in b.get('results')]

print(c)

['https://www.montratec.de/fileadmin/user_upload/footer/montratec_Design_Guide_ENG.pdf']

SimonT
  • 974
  • 1
  • 5
  • 9
  • I made a small edit in the qs. If I use ```response.text.get``` I would get an error because it is a string and not a dictionary. If I just do response.get, I would still get an error because response doesnt have a get property. – x89 Jul 15 '21 at 13:21
  • @SimonT Cannot use `a['results']` instead of `get()` ? – Alexandre Marcq Jul 15 '21 at 13:21
  • Thst doesnt work either bc its a string, not a dict – x89 Jul 15 '21 at 13:22
  • You have to parse your string into a dictionary, see [this](https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary) for more details. – Alexandre Marcq Jul 15 '21 at 13:25
  • @x89 you need to parse the JSON before you can operate on it. – Axe319 Jul 15 '21 at 13:25
  • I've updated my answer to match your edits – SimonT Jul 15 '21 at 13:30
  • 1
    @AlexandreMarcq you can use ['results'] instead if you wanted to but .get ignores keyerrors and returns None if not found. – SimonT Jul 15 '21 at 13:52