import json from bs4 import BeautifulSoup path = ".work/Gambling Man/book_structure.json" target_file = "e9781668070765/xhtml/ch08.xhtml" target_id = "uuid-100309c1-0643-4aa1-8ffb-1d2bfbd0e376" try: with open(path, 'r') as f: data = json.load(f) target_res = None for item_id, res in data['resources'].items(): if "ch08.xhtml" in res.get('href', ''): print(f"Found resource with ID: {item_id}, href: {res.get('href')}") target_res = res break if not target_res: print(f"Resource {target_file} not found by href search.") exit(1) content = target_res['content'] soup = BeautifulSoup(content, 'html.parser') element = soup.find(id=target_id) if element: print(f"--- HTML for {target_id} ---") print(element.prettify()) print("--- Raw ---") print(str(element)) else: print("Element not found") except Exception as e: print(e)