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?
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']