Home > Blockchain >  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, how do I get the value using python?

Time:01-26

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?

CodePudding user response:

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"])
  • Related