把你的beauthulsoup4汤变成python字典

soup2dict的Python项目详细描述


soup2dict公司

BeautifulSoup4到python字典转换器


{1}$ codecovPython Versionwemake-python-styleguide


为什么

很高兴有一个方便的方法把你的汤变成dict

安装

用pip或诗歌获得包裹

pip install soup2dict
^{pr2}$

示例

importsimplejsonfrombs4importBeautifulSoupfromsoup2dictimportconverthtml_doc="""<html>hei<head>    <title>The Dormouse's story</title>    <title>bob</title></head><body>    <p class="title">The <b>Dormouse's story</b></p>    <p class="story">Once upon a time there were three little sisters;    and their names were    <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,    <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and    <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;    and they lived at the bottom of a well.</p>    <p class="story">...</p>"""# Create soup from html_doc datasoup=BeautifulSoup(html_doc,'html.parser')# Convert it to a dictionary with convert()dict_result=convert(soup)withopen('output.json','w')asoutput_file:output_file.write(simplejson.dumps(dict_result,indent=2),)

输出

{"html":[{"#text":"hei The Dormouse's story bob The Dormouse's story Once upon a time there were three little sisters; and their names were Elsie , Lacie and Tillie ; and they lived at the bottom of a well. ...","navigablestring":["hei"],"head":[{"#text":"The Dormouse's story bob","title":[{"#text":"The Dormouse's story","navigablestring":["The Dormouse's story"]},{"#text":"bob","navigablestring":["bob"]}]}],"body":[{"#text":"The Dormouse's story Once upon a time there were three little sisters; and their names were Elsie , Lacie and Tillie ; and they lived at the bottom of a well. ...","p":[{"@class":["title"],"#text":"The Dormouse's story","navigablestring":["The"],"b":[{"#text":"Dormouse's story","navigablestring":["Dormouse's story"]}]},{"@class":["story"],"#text":"Once upon a time there were three little sisters; and their names were Elsie , Lacie and Tillie ; and they lived at the bottom of a well.","navigablestring":["Once upon a time there were three little sisters;\n    and their names were",",","and",";\n    and they lived at the bottom of a well."],"a":[{"@href":"http://example.com/elsie","@class":["sister"],"@id":"link1","#text":"Elsie","navigablestring":["Elsie"]},{"@href":"http://example.com/lacie","@class":["sister"],"@id":"link2","#text":"Lacie","navigablestring":["Lacie"]},{"@href":"http://example.com/tillie","@class":["sister"],"@id":"link3","#text":"Tillie","navigablestring":["Tillie"]}]},{"@class":["story"],"#text":"...","navigablestring":["..."]}]}]}]}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
如何在javaswing中使用进度条,同时函数在繁重的进程中工作   SWIG(Java):如何调用带有void*out参数的函数?   java帮助解决算法问题   java,但我不想用try/catch块来包围语句!   可以从java类调用安卓类吗?   java流拆分器实现细节   java组织。莫基托。例外情况。滥用。无效使用MatcherException   java如何显示接收到的字符串?   java为什么这段代码不像JSR133建议的那样进入无限循环?   java BufferedReader readLine()从socket获取数据时冻结   java如何使用html表单发送的XML?   java RxJava与观察者代码的并行执行   我想使用CoreJava基于文本输入动态创建一个jpg图像   java如何在Mockito中模拟注入的bean?   java我的程序突然停止工作,我没有改变我记得的任何东西,现在它给出了第二个错误:javafx。fxml。加载异常   java树集排序错误   java RSA加密解密AES密钥并存储在文件中   Java将变量字符串[]与字符串混合在一个字符串数组中   如何在鼠标移动时重新绘制Java SWT应用程序?