我有一個網站,我想從中抓取資料。此特定資料位于腳本標簽下,我無法弄清楚如何訪問它。
<script type="text/javascript">
window.gameData = {"a dict of data I would like to get"}
</script>
到目前為止,我可能已經嘗試了“find_element_by ...”的所有組合,我開始懷疑這個問題是否可以解決。它不允許我使用“get_attribute”、“.text”或任何其他有用的 selenium 函式。
我什至嘗試過這個功能
#script = wd.find_element_by_xpath('//*[@id="js-hook-pz-moment__game"]/script/text()')
這是 javascript 加載資料的直接 xpath,它回傳了一個非常可怕的錯誤。
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//*[@id="js-hook-pz-moment__game"]/script/text()" is: [object Text]. It should be an element.
所以這就是我的問題。感謝您的任何幫助:)
uj5u.com熱心網友回復:
你會這樣做:
game_data = driver.execute_script("return window.gameData")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/431616.html
上一篇:必須初始化不可為空的變數“相機”
