Python中通过绝对值获取XML文件中的值

我有一个我想要检索的 XML 文件值的绝对路径。绝对路径的格式为“A/B/C”。我怎样才能在Python中做到这一点?



慕桂英3389331
浏览 91回答 3
3回答

GCT1015

使用ElementTree库(请注意,我的答案使用核心 python 库,而其他答案使用外部库。)要抓取前三个句子,只需将这些行添加到您的代码中:section = soup.find('section',class_ = "article_text post") #Finds the section tag with class "article_text post"txt = section.p.text #Gets the text within the first p tag within the variable section (the section tag)print(txt)输出:Many people will land on this page after learning that their email address has appeared in a data breach I've called "Collection #1". Most of them won't have a tech background or be familiar with the concept of credential stuffing so I'm going to write this post for the masses and link out to more detailed material for those who want to go deeper.希望这有帮助!

繁星coding

另一种方法。from simplified_scrapy import SimplifiedDoc, utils, req# Basicxml = '''<ROOT><A><B><C>The Value</C></B></A></ROOT>'''doc = SimplifiedDoc(xml)print (doc.select('A>B>C'))# Multiplexml = '''<ROOT><A><B><C>The Value 1</C></B></A><A><B><C>The Value 2</C></B></A></ROOT>'''doc = SimplifiedDoc(xml)# print (doc.selects('A').select('B').select('C'))print (doc.selects('A').select('B>C'))# Mixed structurexml = '''<ROOT><A><other>no B</other></A><A><other></other><B>no C</B></A><A><B><C>The Value</C></B></A></ROOT>'''doc = SimplifiedDoc(xml)nodes = doc.selects('A').selects('B').select('C')for node in nodes:&nbsp; for c in node:&nbsp; &nbsp; if c:&nbsp; &nbsp; &nbsp; print (c)结果:{'tag': 'C', 'html': 'The Value'}[{'tag': 'C', 'html': 'The Value 1'}, {'tag': 'C', 'html': 'The Value 2'}]{'tag': 'C', 'html': 'The Value'}

慕慕森

您可以使用lxml,您可以通过安装pip install lxml。from simplified_scrapy import SimplifiedDoc, utils, req# Basicxml = '''<ROOT><A><B><C>The Value</C></B></A></ROOT>'''doc = SimplifiedDoc(xml)print (doc.select('A>B>C'))# Multiplexml = '''<ROOT><A><B><C>The Value 1</C></B></A><A><B><C>The Value 2</C></B></A></ROOT>'''doc = SimplifiedDoc(xml)# print (doc.selects('A').select('B').select('C'))print (doc.selects('A').select('B>C'))# Mixed structurexml = '''<ROOT><A><other>no B</other></A><A><other></other><B>no C</B></A><A><B><C>The Value</C></B></A></ROOT>'''doc = SimplifiedDoc(xml)nodes = doc.selects('A').selects('B').select('C')for node in nodes:  for c in node:    if c:      print (c)结果:{'tag': 'C', 'html': 'The Value'}[{'tag': 'C', 'html': 'The Value 1'}, {'tag': 'C', 'html': 'The Value 2'}]{'tag': 'C', 'html': 'The Value'}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python