B
B
badrbot2018-11-11 17:27:22
Python
badrbot, 2018-11-11 17:27:22

How to extract all links to python from json?

Here is an excerpt from the json file:

"editions":[{"title":"\u0423\u0447\u0435\u0431\u043d\u0438\u043a","description":null,"type":"1","publisher_id":"31","texts":[],"images":[{"title":null,"url":"\/attachments\/images\/tasks\/000\/096\/125\/0002\/5be55bf80f89d.jpg"},{"title":null,"url":"\/attachments\/images\/tasks\/000\/096\/125\/0002\/5be55bf80fca3.jpg"}],"documents":[]},{"title":"\u0420\u0435\u0448\u0435\u0431\u043d\u0438\u043a","description":null,"type":"2","publisher_id":"2","texts":[],"images":[{"title":null,"url":"\/attachments\/images\/tasks\/000\/096\/125\/0002\/5ba0f2d96dc89.jpg"},{"title":null,"url":"\/attachments\/images\/tasks\/000\/096\/125\/0002\/5ba0f2d96e060.jpg"},{"title":null,"url":"\/attachments\/images\/tasks\/000\/096\/125\/0002\/5ba0f2d96e5e4.jpg"},{"title":null,"url":"\/attachments\/images\/tasks\/000\/096\/125\/0002\/5ba0f2d96eaa3.jpg"}

I can only pull the first link
def get_url_for_img(url):
    s=requests.get(url) 
    data = s.json()
    k = []
    k = data['editions'][0]['images'][0]['url']
    return k#/attachments/images/tasks/000/096/125/0002/5be55bf80f89d.jpg

and I will need to parse all links, and there are about a thousand such json files and each has a different number of links

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2018-11-11
@acrytzat

def get_url_for_img(url):
    data = requests.get(url).json()

    for edition in data['editions']:
        for image in edition['images']:
            yield image['url']

Or easier:
def get_url_for_img(url):
    data = requests.get(url).json()
    result = []
    for edition in data['editions']:
        for image in edition['images']:
            result.append(image['url'])
    return result

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question