import urllib.request import json import re from bs4 import BeautifulSoup import sys sys.stdout.reconfigure(encoding='utf-8') url = 'https://klokah.iformosa.com.tw/parent_child/d33/s1/l1/talk' req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}) html = urllib.request.urlopen(req).read().decode('utf-8') soup = BeautifulSoup(html, 'html.parser') for s in soup.find_all('script'): if s.string and 'window.tData' in s.string: match = re.search(r'window\.tData\s*=\s*"(.*?)";', s.string) if match: # The JSON string is double-escaped, let's decode it safely raw_data = match.group(1).encode('utf-8').decode('unicode_escape') data = json.loads(raw_data) print(json.dumps(data, ensure_ascii=False, indent=2))