如何使用python将html文件转换为json

如何使用python将html文件转换为json,python,html,json,Python,Html,Json,我想从某个位置获取html文件,并使用python将其转换为Json格式 对于下面的代码,我得到的输出只是一个文本 from bs4 import BeautifulSoup import json html = '<p>Hello</p><p>world</p>' soup = BeautifulSoup(html, 'html.parser') things = soup.find_all(text=True) print(things) 从

我想从某个位置获取html文件,并使用python将其转换为Json格式

对于下面的代码,我得到的输出只是一个文本

from bs4 import BeautifulSoup
import json
html = '<p>Hello</p><p>world</p>'
soup = BeautifulSoup(html, 'html.parser')
things = soup.find_all(text=True)
print(things)
从bs4导入美化组
导入json
html='Hello

world

' soup=BeautifulSoup(html,'html.parser') 事物=汤。全部查找(text=True) 印刷品(物品)
这个答案似乎是直接从这里提出来的:是的,因为问题的原因是相同的,而且还在起作用,所以@mohan111大多数用户都避免使用开放链接,所以你最好直接提出建议,这就是我为什么在@mohan111使用它的原因
 jsonD = json.dumps(htmlContent.text) converts the raw HTML content into a JSON 
 string representation. jsonL = json.loads(jsonD) parses the JSON string back into a 
 regular string/unicode object. This results in a no-op, as any escaping done by 
 dumps() is reverted by loads(). jsonL contains the same data as htmlContent.text.

 Try to use json.dumps to generate your final JSON instead of building the JSON by 
 hand:

 ContentUrl = json.dumps({
'url': str(urls),
'uid': str(uniqueID),
'page_content': htmlContent.text,
'date': finalDate
})