yoohoo1234
yoohoo1234

Reputation: 3

Extracting URLs from JSON file with python

The JSON file is like this:

[
        {
            "src": "https://",
            "viewcount": "0"
        },
        {
            "src": "https://",
            "viewcount": "0"
        }

]  

I wish to extract all the values under src (all of which are urls) with python.

How can I do so?

Thank you.

Upvotes: 0

Views: 246

Answers (2)

Joshua
Joshua

Reputation: 561

Try loading JSON first.

Sample output:

enter image description here

import json

myjsonstr =  '[{"src": "https://google.com","viewcount": "0"},{"src": "https://yahoo.com","viewcount": "0"}]'

myjsonobj = json.loads(myjsonstr)

for eachitem in myjsonobj:
    print(eachitem["src"])

Upvotes: 1

Edoardo Facchinelli
Edoardo Facchinelli

Reputation: 422

You're looking for the json module. loads will try to parse a string (watch out, there's an extra/missing bracket in what you posted).

After that, it's a regular dict.

import json

source = """[
        {
            "src": "https://",
            "viewcount": "0"
        },
        {
            "src": "https://",
            "viewcount": "0"
        }
    ]  
"""

urls = [x['src'] for x in json.loads(j)]
['https://', 'https://']

Upvotes: 0

Related Questions