Extract JSON from HTML Script tag with BeautifulSoup in Python
问题 I have the following HTML, and what should I do to extract the JSON from the variable: window.__INITIAL_STATE__ <!DOCTYPE doctype html> <html lang="en"> <script> window.sessConf = "-2912474957111138742"; /* <sl:translate_json> */ window.__INITIAL_STATE__ = { /* Target JSON here with 12 million characters */}; /* </sl:translate_json> */ </script> </html> 回答1: You can use the following Python code to extract the JavaScript code. soup = BeautifulSoup(html) s=soup.find('script') js = 'window = {}