[Python] I cannot access data from CDATA [closed]

Discussão em 'Python' iniciado por Stack, Junho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to get data from a car biding website. I find this in the HTML of a website,

    /* <![CDATA[ */ var auctionsCompletedInitialData = {"items":[{"active":false,"categories":null,"comments":"0","current_bid":180000,"current_bid_formatted":"USD $180,000","id":25870485,...]...}

    Inside of this auctionsCompletedInitialData there this "all":[{"id":93202081},{"id":93802811}...] and I want to get all the id numbers. But for some reason I cannot access the id numbers.

    I not really sure what I am doing so most of my code is just ai slop.

    from bs4 import BeautifulSoup
    import requests
    import json
    import demjson3
    import re

    response = requests.get(URL)


    soup = BeautifulSoup(response.text, 'html.parser')

    # Step 1: Find the script tag containing "auctionsCompletedInitialData"
    script_tag = soup.find('script', string=re.compile(r'auctionsCompletedInitialData'))

    if script_tag:
    script_text = script_tag.string or script_tag.text

    # Step 2: Extract JS object assigned to auctionsCompletedInitialData
    match = re.search(
    r'var\s+auctionsCompletedInitialData\s*=\s*(\{.*?\});',
    script_text,
    re.DOTALL
    )

    if match:
    js_object_str = match.group(1)

    # Step 3: Parse the JS object into Python dict using demjson3
    try:
    data = demjson3.decode(js_object_str)

    # Step 4: Extract IDs
    item_ids = [item['id'] for item in data.get('items', []) if 'id' in item]
    all_ids = [entry['id'] for entry in data.get('all', []) if 'id' in entry]

    print("IDs from items:", item_ids)
    print("IDs from all:", all_ids)
    except demjson3.JSONDecodeError as e:
    print("Could not decode JS object:", e)
    else:
    print("Couldn't extract JS object from script.")
    else:
    print("Script tag with data not found.")


    My problem is that I am able to get the ids from the 'items', but form some reason I am not able to extract any ids from 'all'

    Continue reading...

Compartilhe esta Página