我正在使用python和beautifulsoup,我需要在app.run({"data":Id":"124330049","stock":83})
下提取一些数据。当我试图找到这些值时,它使用find返回一个空列表。
我可以在app.run中提取/抓取数据吗?
码:
soup = content.find('script').get_text()
found_data = json.loads(soup)
print(found_data)
//这给了我错误:需要输出股票:83
您可以选择具有特定子字符串的所有脚本标记,然后根据需要进行正则表达式
import re
from bs4 import BeautifulSoup as bs
html = '''
<script type="text/javascript">app.run({"data":Id":"124330049","stock":83})</script>
'''
soup = bs(html, 'lxml')
scripts = [script.text for script in soup.select('script') if 'app.run({"data":Id"' in script.text]
r = re.compile(r'"stock":(\d+)}')
for script in scripts:
print(r.findall(script))