希望从下面的代码转换嵌套字典。你知道吗
import requests
from bs4 import BeautifulSoup
url = 'https://www.bundesbank.de/en/statistics/time-series-databases/time-series-databases/743796/openAll?treeAnchor=BANKEN&statisticType=BBK_ITS'
result = requests.get(url)
soup = BeautifulSoup(result.text, 'html.parser')
def get_child_nodes(parent_node):
node_name = parent_node.a.get_text(strip=True)
result = {"name": node_name, "children": []}
children_list = parent_node.find('ul', recursive=False)
if not children_list:
return result
for child_node in children_list('li', recursive=False):
result["children"].append(get_child_nodes(child_node))
return result
Data_Dict = get_child_nodes(soup.find("div", class_="statisticTree"))
是否可以如图所示导出父-子关系?你知道吗
上面的代码来自@alecxe:Fetch complete List of Items using BeautifulSoup, Python 3.6的答案
我试过了,但太复杂了,无法理解,请帮助相同的。你知道吗
字典:http://s000.tinyupload.com/index.php?file_id=97731876598977568058
字典数据示例:
{"name": "Banks", "children": [{"name": "Banks", "children": [{"name": "Balance sheet items", "children":
[{"name": "Minimum reserves", "children": [{"name": "Reserve maintenance in the euro area", "children": []}, {"name": "Reserve maintenance in Germany", "children": []}]},
{"name": "Bank Lending Survey (BLS) - Results for Germany", "children": [{"name": "Lending", "children": [{"name": "Enterprises", "children": [{"name": "Changes over the past three months", "children": [{"name": "Credit standards and explanatory factors", "children": [{"name": "Overall", "children": []}, {"name": "Loans to small and medium-sized enterprises", "children": []}, {"name": "Loans to large enterprises", "children": []}, {"name": "Short-term loans", "children": []}, {"name": "Long-term loans", "children": []}]}, {"name": "Terms and conditions and explanatory factors", "children": [{"name": "Overall", "children": [{"name": "Overall terms and conditions and explanatory factors", "children": []}, {"name": "Margins on average loans and explanatory factors", "children": []}, {"name": "Margins on riskier loans and explanatory factors", "children": []}, {"name": "Non-interest rate charges", "children": []}, {"name": "Size of the loan or credit line", "children": []}, {"name": "Collateral requirements", "children": []}, {"name": "Loan covenants", "children": []}, {"name": "Maturity", "children": []}]}, {"name": "Loans to small and medium-sized enterprises", "children": []}, {"name": "Loans to large enterprises", "children": []}]}, {"name": "Share of enterprise rejected loan applications", "children": []}]}, {"name": "Expected changes over the next three months", "children": [{"name": "Credit standards", "children": []}]}]}, {"name": "Households", "children": [{"name": "Changes over the past three months", "children": [{"name": "Credit standards and explanatory factors", "children": [{"name": "Loans for house purchase", "children": []}, {"name": "Consumer credit and other lending", "children": []}]},
您可以使用递归函数来处理这个问题。你知道吗
然后,您可以选择创建数据帧,或立即导出到csv,如示例输出中所示。要创建数据帧,我们只需执行以下操作:
给予:
或者要输出到csv,我们可以使用^{} 内置库:
输出:
相关问题 更多 >
编程相关推荐