我可以在app.run中刮取或提取数据吗

2024-04-25 01:46:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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) 

//这给了我一个错误:需要输出stock:83


Tags: 数据run代码idapp列表dataget
1条回答
网友
1楼 · 发布于 2024-04-25 01:46:22

您可以选择所有的脚本标记,其中包含一个特定的子字符串,然后选择regex

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))

相关问题 更多 >