【发布时间】:2021-07-10 20:40:23
【问题描述】:
我正在尝试从此标签中获取 hlsUrl。
import json
import re
from bs4 import BeautifulSoup
html_doc = """
<script>
var modelData = {
"hlsUrl": "null",
"account": "1V2FO4K7ME78RV09VXNEC",
"packageName": "null",
isActive: false
}
</script>
"""
soup = BeautifulSoup(html_doc, "html.parser")
script_text = soup.select_one("script").string
model_data = re.search(r"modelData = ({.*?})", script_text, re.S).group(1)
print(json.loads(model_data)["account"])
但我收到此错误:
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 5 column 9 (char 111)
我知道这是因为 isActive: false 导致它不是有效的 json
如何将其转换为有效的 json 或以其他方式获取 hlsUrl?
【问题讨论】:
标签: python json beautifulsoup