Home > Net >  Python print only .jpg url's from json
Python print only .jpg url's from json

Time:11-05

How can I print only .jpg/.png urls from API using json?

`

import requests
import json

r = requests.get("https://random.dog/woof.json")
print("Kod:", r.status_code)

def jprint(obj):
    text = json.dumps(obj, sort_keys=True, indent=4)
    print(text)

jprint(r.json())

`

Results:

`

{
    "fileSizeBytes": 78208,
    "url": "https://random.dog/24141-29115-27188.jpg"
}

`

I tried .endswith() but without any success. I'm beginner.

CodePudding user response:

This example will use str.endswith to check, if dictionary value under the key url contains the string .jpg or .png.

I'm doing 10 requests and using time.sleep to wait 1 second between each request:

import requests
from time import sleep

url = "https://random.dog/woof.json"

for i in range(10):
    print("Attempt {}".format(i   1))

    data = requests.get(url).json()
    if data["url"].endswith((".jpg", ".png")):
        print(data["url"])

    sleep(1)

Prints (for example, data returned from the server is random):

Attempt 1
https://random.dog/aa8e5e24-5c58-4963-9809-10f4aa695cfc.jpg
Attempt 2
https://random.dog/5b2a4e74-58da-4519-a67b-d0eed900b676.jpg
Attempt 3
https://random.dog/56217498-0e6b-4c24-bdd1-cc5dbb2201bb.jpg
Attempt 4
https://random.dog/l6CIQaS.jpg
Attempt 5
Attempt 6
Attempt 7
https://random.dog/3b5eae93-b3bd-4012-b789-64eb6cdaac65.png
Attempt 8
https://random.dog/oq9izk0057hy.jpg
Attempt 9
Attempt 10

CodePudding user response:

Try to use .lower() as endswith is case sensitive that's why when you use .endswith() it's not working. because some data it has upper case file format.

def jprint(obj):
text = json.dumps(obj, sort_keys=True, indent=4)
if 'url' in obj and obj['url'] and obj['url'].lower().endswith((".jpg", ".png")):
    print(obj['url'])

CodePudding user response:

This code should do it:

import requests
import json

r = requests.get("https://random.dog/woof.json")
print("Kod:", r.status_code)

def jprint(obj):
    text = json.dumps(obj, sort_keys=True, indent=4)
    print(text)

jprint(r.json())

to_remove = []

for key, item in r.json().items():
    try:
        if not (item.endswith(".png") or item.endswith(".gif") or item.endswith(".jpg")  or item.endswith(".mp4")):
            to_remove.append([key, item]) # The key is useless- delete it later
    except AttributeError: # the value is not a string!
        to_remove.append([key, item]) # The key is useless- delete it later
        
new_r = {}
for key, item in r.json().items():
    if not [key, item] in to_remove:
        new_r[key] = item # item was not in to_remove, so add it to the new filtered dict.

jprint(new_r)

Explanation

After we get the request, we loop through the r.json() dict, and if the value is either an int or does not end with a photo/video extension, add the key and value to an array. Then create a new dict, and store all values from the original dictionary (r) which are not mentioned in the array.

Example Output

Kod: 200
{
    "fileSizeBytes": 2896573,
    "url": "https://random.dog/2fc649b9-f688-4e65-a1a7-cdbc6228e0c0.mp4"
}
{
    "url": "https://random.dog/2fc649b9-f688-4e65-a1a7-cdbc6228e0c0.mp4"
}
  • Related