[英]How to make a for loop iterate through each item in a string with an if statement?
[英]Beautifulsoup: Soupy runny xml, single loop iterate through each item
假設您有一些XML的結構如下,但是使用這些標簽時可以采用任何形狀,且標簽名稱更深,並且可能以怪異的方式重復使用:
<a>
<b>
<c />
</b>
<b>
<c />
</b>
<b>
<b>
<d>
<b>
<e>
<f>
<c />
</f>
</e>
</b>
<b>
<e>
<f>
<c />
</f>
</e>
</b>
</d>
</b>
</b>
<b>
<b>
<c />
</b>
</b>
</a>
我想讓它們按從上到下的順序逐一遍歷每個標簽,重復的標簽可以按任何順序或結構使用。 我想使用beautifulsoup逐個瀏覽每個標簽。 例如:
soup = BeautifulSoup(xmlcode, "xml")
for asd in soup.findAll(True, recursive=False):
print asd.prettify()
print "---------"
所有這些返回都是一個大的bs4.element.Tag。 我希望它按出現的順序返回19行。 基本上,我要做的就是希望使用單個循環或盡可能少的循環遍歷每個單個標簽。 我可能會提供比beautifulsoup更好的選擇。
您正在尋找.children
:
xml_soup = BeautifulSoup(xml, "xml")
for tag in xml_soup.children:
print tag
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.