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:

how to get value image using python?

Asked By: Si Doel



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:
Answered By: Barmar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.