python - 使用beautifulsoup查找没有标签的json对象
问题描述
假设我们有以下
<title> Hello world! </title>
<span class="something">Here is some text</span>
{foo:{bar:1},alpha:2,beta:{donkey:horse}}
我想在python中提取字典/json对象。由于它不在标签中,因此我很难soup.find_all
以一种简单的方式使用它 - 是使用正则表达式的最佳方式,还是有更好的主意?请注意,dict 可以在网页上的任何位置,即并不总是作为网页上的最后一部分
解决方案
你可以尝试这样的事情。假设“beta”是每个 json 对象
h = ''''<title> Hello world! </title>
<span class="something">Here is some text</span>
{foo:{bar:1},alpha:2,beta:{donkey:horse}}'''
soup = BeautifulSoup(h, "html.parser")
soup.find_all(string=re.compile('beta'))
这是输出(列表)。分页符很可能来自如何将其作为字符串读取。
['\n{foo:{bar:1},alpha:2,beta:{donkey:horse}}']
推荐阅读
- c# - WCF:在 depyloing 到本地 iis 后未找到 HTTP 404.17
- r - `geom_text()` 标签非常浅/淡 - 需要它们正常/深色
- python - 部署到 GCP 时,支持 mysql 的 Django 应用程序无法正常工作
- json - 如何在 Xamarin 中使用 PUT 方法
- unity3d - 为什么我无法将动画剪辑添加到检查器中角色动画选项卡的列表中?
- r - 使用熔化/铸造重新排列数据框的问题
- java - 为什么键绑定不能与 CardLayout 一起使用?
- junit - UIAutomator - editText 中的 setText 不起作用
- sql-server - 子查询适用于这种情况吗?
- java - 如何读取 .jar 中的文件,该文件是 .war 文件的依赖项