XML xml 中相同兄弟姐妹的 2 倍,我如何获得第二个?
XML 2x the same siblings in xml, how do i get the second one?
我已经 xml 提要,我想从 xml:
的这一行中获取类别和子类别
与:
cat = x.find('categories/category/cat/title').text
我只得到第一个(vibo's
),还需要(Vibrator Speciaal
)
<categories>
<category>
<cat>
<id>1</id>
<title>Vibo's</title> //Need this one
</cat>
<cat>
<id>182</id>
<title>Vibrator Speciaal</title> //and need this one
</cat>
</category>
</categories>
无法按我的意愿运行
我认为您可以使用 lxml 和 XPath 表达式来做到这一点:
from lxml import etree
tree = etree.parse("yourXMLFile.xml")
for title in tree.xpath("/categories/category/cat/title"):
print(title.text)
如果您使用 BeautifulSoup
,那么您应该使用 find_all
而不是 find
。
cat = soup.find_all('title')
它给出了包含所有元素的列表,然后你可以使用for
-loop
for item in cat:
print(item.text)
或索引或切片
print(cat[1].text)
编辑: 在其他模块中它可能有名称 findall
text = """
<categories>
<category>
<cat>
<id>1</id>
<title>Vibo's</title> //Need this one
</cat>
<cat>
<id>182</id>
<title>Vibrator Speciaal</title> //and need this one
</cat>
</category>
</categories>
"""
# -----
from bs4 import BeautifulSoup
soup = BeautifulSoup(text, 'html.parser')
cat = soup.find_all('title')
for item in cat:
print(item.text)
# OR
print(cat[1].text)
# -----
import lxml.etree
soup = lxml.etree.fromstring(text)
cat = soup.findall('.//title')
for item in cat:
print(item.text)
# OR
print(cat[1].text)
感谢您的快速回答!
这对我来说非常完美!感谢您的帮助
cat = x.findall('categories/category/cat/title')
print(blabla, blabla, blabla, cat[1].text)
print(blabla, blabla, blabla, cat[0].text)
我已经 xml 提要,我想从 xml:
的这一行中获取类别和子类别与:
cat = x.find('categories/category/cat/title').text
我只得到第一个(vibo's
),还需要(Vibrator Speciaal
)
<categories>
<category>
<cat>
<id>1</id>
<title>Vibo's</title> //Need this one
</cat>
<cat>
<id>182</id>
<title>Vibrator Speciaal</title> //and need this one
</cat>
</category>
</categories>
无法按我的意愿运行
我认为您可以使用 lxml 和 XPath 表达式来做到这一点:
from lxml import etree
tree = etree.parse("yourXMLFile.xml")
for title in tree.xpath("/categories/category/cat/title"):
print(title.text)
如果您使用 BeautifulSoup
,那么您应该使用 find_all
而不是 find
。
cat = soup.find_all('title')
它给出了包含所有元素的列表,然后你可以使用for
-loop
for item in cat:
print(item.text)
或索引或切片
print(cat[1].text)
编辑: 在其他模块中它可能有名称 findall
text = """
<categories>
<category>
<cat>
<id>1</id>
<title>Vibo's</title> //Need this one
</cat>
<cat>
<id>182</id>
<title>Vibrator Speciaal</title> //and need this one
</cat>
</category>
</categories>
"""
# -----
from bs4 import BeautifulSoup
soup = BeautifulSoup(text, 'html.parser')
cat = soup.find_all('title')
for item in cat:
print(item.text)
# OR
print(cat[1].text)
# -----
import lxml.etree
soup = lxml.etree.fromstring(text)
cat = soup.findall('.//title')
for item in cat:
print(item.text)
# OR
print(cat[1].text)
感谢您的快速回答!
这对我来说非常完美!感谢您的帮助
cat = x.findall('categories/category/cat/title')
print(blabla, blabla, blabla, cat[1].text)
print(blabla, blabla, blabla, cat[0].text)