BeautifulSoup並非為此目的而構建。 它用於解析html而不是JavaScript或任何其他網絡語言。
更簡單的方法是使用無頭瀏覽器,例如PhantomJS或Selenium
您也可以使用JavaScript的解析正則表達式中的說明,或者沒有正則表達式這個帖子
編輯:
例:
我不知道如何使用無頭瀏覽器,雖然這可能是最好的方法,但是我知道足夠的正則表達式來創建一個快速而骯臟的示例,該示例將適用於您在上面列出的示例。
b = """function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = this.responseText; } }; xhttp.open("GET", "ajax_info.txt", true); xhttp.send(); }""" # Get value between <script></script> using BeautifulSoup and store in "b" import re a = re.compile('"GET", "[\\S ]+"') c = a.search(b).group() print(c[8:-1])