Download parse_tdata.py from ILRDF-Lowking/truku-interactive-lesson: direct link, hf CLI and curl.
- Browser
- Download file 794 Bytes
-
https://huggingface.co/spaces/ILRDF-Lowking/truku-interactive-lesson/resolve/main/parse_tdata.py
- Command line
-
hf download hf://spaces/ILRDF-Lowking/truku-interactive-lesson/parse_tdata.py
-
curl -L -o parse_tdata.py https://huggingface.co/spaces/ILRDF-Lowking/truku-interactive-lesson/resolve/main/parse_tdata.py
794 Bytes
| 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)) | |