如何使用Python和Beautifulsoup从脚本标签获取JavaScript变量 [英] How to get JavaScript variables from a script tag using Python and Beautifulsoup
问题描述
我想使用beautifulsoup和python从变量meta返回"id"值.这可能吗?另外,我不知道如何找到包含meta变量的某些脚本"标签,因为它没有唯一的标识符,以及网站上的许多其他脚本"标签.我也正在使用硒,因此我可以理解任何答案.
I want to return the "id" value from the variable meta using beautifulsoup and python. This possible? Additionally, I don't know how to find the certain 'script' tag that contains the meta variable because it does not have a unique identifier, as well as many other 'script' tags on the site. I'm also using selenium as well, so I can understand any answers with that.
<script>
var meta = "variants":[{"id":12443604615241,"price":14000},
{"id":12443604648009,"price":14000}]
</script>
推荐答案
如果您使用的是硒,则无需解析html以获取js变量,只需使用硒 webdriver.execute_script()
使其进入python:
If you are using selenium there's no need to parse the html to get the js variable, just use selenum webdriver.execute_script()
to get it to python:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://whatever.com/')
meta = driver.execute_script('return meta')
就这样,meta现在拥有js变量,并且保持其类型
And thats it, meta now holds the js variable, and it maintains its type
这篇关于如何使用Python和Beautifulsoup从脚本标签获取JavaScript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!