我有一个XML文件,在运行我的美丽汤findAll(“命名-查询”)并打印出来之后,我得到了如下结果:
<named-query name="sdfsdfsdf">
<query>
---Query here...--
</query>
</named-query>
<named-query name="xkjlias">
<query>
---Query here...--
</query>
</named-query>
.
.
.是否有办法将其转换为字典、json或csv,例如:
name="sdfsdfsdf“查询=.
name="xkjlias“查询=。
提前谢谢。
发布于 2020-02-21 20:00:06
代码:
import json
from bs4 import BeautifulSoup
text = """
<named-query name="sdfsdfsdf">
<query>
---Query here...--
</query>
</named-query>
<named-query name="xkjlias">
<query>
---Query here2...--
</query>
</named-query>"""
soup = BeautifulSoup(text, 'html.parser')
queries = {nq.attrs['name']: nq.text.strip() for nq in soup.find_all('named-query')}
queries_json = json.dumps(queries)
print(queries) # dict
print(queries_json) # json输出:
{'sdfsdfsdf': '---Query here...--', 'xkjlias': '---Query here2...--'}
{"sdfsdfsdf": "---Query here...--", "xkjlias": "---Query here2...--"}发布于 2020-02-21 19:57:59
试试这个:
# initialize a dictionary
data = {}
# for each tag 'named-query
for named_query in soup.findAll('named-query'):
# get the value of name attribute and store it in a dict
data['name'] = named_query.attrs['name']
# traverse its children
for child in named_query.children:
# check for '\n' and empty strings
if len(child.string.strip()) > 0:
data['query'] = child.string.strip()
print (data)>>> {'name': 'sdfsdfsdf', 'query': '---Query here...--'}https://stackoverflow.com/questions/60345046
复制相似问题