Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have ld+json, and I want to get the image value, how do I get the value using python?

I have ld+json, and I want to get the image value using python

import json
import request
from bs4 import BeautifulSoup

url = f'https://www.tiket.com/hotel/indonesia/ideas-hotel-bandung-108001534490330380?checkin=2023-01-26&checkout=2023-01-27&room=1&adult=1&soldOut='

    # Set the headers
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'}

soup = BeautifulSoup(requests.get(url).content,'html.parser')
data = [json.loads(x.string) for x in soup.find_all('script', type= "application/ld+json")]

for d in data:
    print(d.get("images"))

how to get value image using python?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First, the JSON is an array (notice the [] around it), you need to loops over the elements. Second, there’s no images key, it’s just image.

for d in data:
    for obj in d:
        if "image" in obj:
            print(obj["image"])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading