Python Beaufifulsou,lxml导航,用于带“的标签-&引用;在标签名中? > > .... 文本> 文本 文本 文本 ... 文本> 文本 文本> 文本

Python Beaufifulsou,lxml导航,用于带“的标签-&引用;在标签名中? > > .... 文本> 文本 文本 文本 ... 文本> 文本 文本> 文本,python,python-3.x,beautifulsoup,lxml,Python,Python 3.x,Beautifulsoup,Lxml,嗨 我正在尝试使用Python BeautifulSoup和lxml解析上述xml文件。通常我使用“.”导航到元素。 e、 g 从bs4导入美化组 使用open('sample.xml','r')作为xml\u文件: soup=BeautifulSoup(xml_文件'lxml') 对于soup.body.response.policy.panorama.address.find('entry')中的项目: 我的问题是通过上面的导航找到像“”和“”这样的标记。由于标记名中有“-”,因此“.”导

我正在尝试使用Python BeautifulSoup和lxml解析上述xml文件。通常我使用“.”导航到元素。 e、 g

从bs4导入美化组
使用open('sample.xml','r')作为xml\u文件:
soup=BeautifulSoup(xml_文件'lxml')
对于soup.body.response.policy.panorama.address.find('entry')中的项目:

我的问题是通过上面的导航找到像“”和“”这样的标记。由于标记名中有“-”,因此“.”导航不起作用。另外,由于子标记具有相同的名称,我不能直接使用它查找。如何在“即”标记下导航和遍历标记?

您可能可以这样做:

from bs4 import BeautifulSoup
with open('sample.xml', 'r') as xml_file:
    soup = BeautifulSoup(xml_file, 'lxml')

for item in soup.body.response.policy.panorama.address.find('entry'):
    <some code action>
输出:

from lxml import etree
rules = """[your xml, fixed]"""
doc = etree.XML(rules)
for i in doc.xpath('//post-rulebase//entry'):
    print(i.tag,i.attrib['name'])
    for t in i.xpath('.//*'):
        print(t.tag,t.text)

您可能可以这样做:

from bs4 import BeautifulSoup
with open('sample.xml', 'r') as xml_file:
    soup = BeautifulSoup(xml_file, 'lxml')

for item in soup.body.response.policy.panorama.address.find('entry'):
    <some code action>
输出:

from lxml import etree
rules = """[your xml, fixed]"""
doc = etree.XML(rules)
for i in doc.xpath('//post-rulebase//entry'):
    print(i.tag,i.attrib['name'])
    for t in i.xpath('.//*'):
        print(t.tag,t.text)
另一种方法

entry some text
tag1 text
tag2 text
entry more text
tag1 text
tag2 text
从simplified\u scrapy导入SimplifiedDoc、req、utils
html=“”
另一种方法

entry some text
tag1 text
tag2 text
entry more text
tag1 text
tag2 text
从simplified\u scrapy导入SimplifiedDoc、req、utils
html=“”

您需要查找哪些信息?请编辑您的问题并在那里发布预期的输出。您所说的“.”导航不工作是什么意思?我需要在下面找到标记。很抱歉,xml复制/粘贴缺少几个标记。你需要找到什么信息?请编辑您的问题并在那里发布预期的输出。您所说的“.”导航不工作是什么意思?我需要在下面找到标记。很抱歉,xml复制/粘贴缺少几个标记。