欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python如何提取xml指定內(nèi)容

 更新時間:2023年01月03日 15:56:43   作者:懿曲折扇情  
這篇文章主要介紹了python如何提取xml指定內(nèi)容,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

第一種方法:python操作xml文件

隨手找了一個xml文件內(nèi)容(jenkins相關(guān)文件)

<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts, id:digerata, Yahoo! Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1"
         metadata-complete="true">
  <display-name>Jenkins v2.336</display-name>
  <description>Build management system</description>
 
  <servlet>
    <servlet-name>Stapler</servlet-name>
    <servlet-class>org.kohsuke.stapler.Stapler</servlet-class>
    <init-param>
      <param-name>default-encodings</param-name>
      <param-value>text/html=UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>diagnosticThreadName</param-name>
      <param-value>false</param-value>
    </init-param>
    <async-supported>true</async-supported>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>Stapler</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
 
  <filter>
    <filter-name>suspicious-request-filter</filter-name>
    <filter-class>jenkins.security.SuspiciousRequestFilter</filter-class>
    <async-supported>true</async-supported>
  </filter>
  <filter>
    <filter-name>diagnostic-name-filter</filter-name>
    <filter-class>org.kohsuke.stapler.DiagnosticThreadNameFilter</filter-class>
    <async-supported>true</async-supported>
  </filter>
  <filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>hudson.util.CharacterEncodingFilter</filter-class>
    <async-supported>true</async-supported>
  </filter>
  <filter>
    <filter-name>compression-filter</filter-name>
    <filter-class>org.kohsuke.stapler.compression.CompressionFilter</filter-class>
    <async-supported>true</async-supported>
  </filter>
  <filter>
    <filter-name>authentication-filter</filter-name>
    <filter-class>hudson.security.HudsonFilter</filter-class>
    <async-supported>true</async-supported>
  </filter>
  <filter>
    <filter-name>csrf-filter</filter-name>
    <filter-class>hudson.security.csrf.CrumbFilter</filter-class>
    <async-supported>true</async-supported>
  </filter>
  <filter>
    <filter-name>plugins-filter</filter-name>
    <filter-class>hudson.util.PluginServletFilter</filter-class>
    <async-supported>true</async-supported>
  </filter>
 
  <!--
	The Headers filter allows us to override headers sent by the container
	that may be in conflict with what we want.  For example, Tomcat will set
	Cache-Control: no-cache for any files behind the security-constraint
	below.  So if Hudson is on a public server, and you want to only allow
	authorized users to access it, you may want to pay attention to this.
	
	See: http://www.nabble.com/No-browser-caching-with-Hudson- -tf4601857.html
  
  <filter>
    <filter-name>change-headers-filter</filter-name>
    <filter-class>hudson.ResponseHeaderFilter</filter-class>
    <!- The value listed here is for 24 hours.  Increase or decrease as you see 
    fit.  Value is in seconds. Make sure to keep the public option ->
    <init-param>
      <param-name>Cache-Control</param-name>
      <param-value>max-age=86400, public</param-value>
    </init-param>
    <!- It turns out that Tomcat just doesn't want to let
    go of its cache option.  If you override Cache-Control,
    it starts to send Pragma: no-cache as a backup.
     ->
    <init-param>
      <param-name>Pragma</param-name>
      <param-value>public</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>change-headers-filter</filter-name>
    <url-pattern>*.css</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>change-headers-filter</filter-name>
    <url-pattern>*.gif</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>change-headers-filter</filter-name>
    <url-pattern>*.js</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>change-headers-filter</filter-name>
    <url-pattern>*.png</url-pattern>
  </filter-mapping>
  -->
 
  <filter-mapping>
    <filter-name>suspicious-request-filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>diagnostic-name-filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>compression-filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>authentication-filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>csrf-filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>plugins-filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
 
  <listener>
    <!-- Must be before WebAppMain in order to initialize the context before the first use of this class. -->
    <listener-class>jenkins.util.SystemProperties$Listener</listener-class>
  </listener>
  <listener>
    <listener-class>hudson.WebAppMain</listener-class>
  </listener>
  <listener>
    <listener-class>jenkins.JenkinsHttpSessionListener</listener-class>
  </listener>
 
  <!--
    JENKINS-1235 suggests containers interpret '*' as "all roles defined in web.xml"
    as opposed to "all roles defined in the security realm", so we need to list some
    common names in the hope that users will have at least one of those roles.
  -->
  <security-role>
    <role-name>admin</role-name>
  </security-role>
  <security-role>
    <role-name>user</role-name>
  </security-role>
  <security-role>
    <role-name>hudson</role-name>
  </security-role>
 
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Hudson</web-resource-name>
      <url-pattern>/loginEntry</url-pattern>
      <!--http-method>GET</http-method-->
    </web-resource-collection>
    <auth-constraint>
      <role-name>**</role-name>
    </auth-constraint>
  </security-constraint>
  
  <!-- Disable TRACE method with security constraint (copied from jetty/webdefaults.xml) -->
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Disable TRACE</web-resource-name>
      <url-pattern>/*</url-pattern>
      <http-method>TRACE</http-method>
    </web-resource-collection>
    <auth-constraint />
  </security-constraint>
  
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>other</web-resource-name>
      <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <!-- no security constraint --> 
  </security-constraint>
 
  <login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
      <form-login-page>/login</form-login-page>
      <form-error-page>/loginError</form-error-page>
    </form-login-config>
  </login-config>
 
 
  <!-- if specified, this value is used as the Hudson home directory -->
  <env-entry>
    <env-entry-name>HUDSON_HOME</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value></env-entry-value>
  </env-entry>
 
  <!-- configure additional extension-content-type mappings -->
  <mime-mapping>
    <extension>xml</extension>
    <mime-type>application/xml</mime-type>
  </mime-mapping>
  <!--mime-mapping> commenting out until this works out of the box with JOnAS. See  http://www.nabble.com/Error-with-mime-type%2D-%27application-xslt%2Bxml%27-when-deploying-hudson-1.316-in-jonas-td24740489.html
    <extension>xsl</extension>
    <mime-type>application/xslt+xml</mime-type>
  </mime-mapping-->
  <mime-mapping>
    <extension>log</extension>
    <mime-type>text/plain</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>war</extension>
    <mime-type>application/octet-stream</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>ear</extension>
    <mime-type>application/octet-stream</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>rar</extension>
    <mime-type>application/octet-stream</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>webm</extension>
    <mime-type>video/webm</mime-type>
  </mime-mapping>
 
  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/oops</location>
  </error-page>
 
  <session-config>
    <cookie-config>
      <!-- See https://www.owasp.org/index.php/HttpOnly for the discussion of this topic in OWASP -->
      <http-only>true</http-only>
    </cookie-config>
    <!-- Tracking mode is managed by WebAppMain.FORCE_SESSION_TRACKING_BY_COOKIE_PROP -->
  </session-config>
</web-app>

提取某個單個字段

# coding=utf-8
"""
    作者:gaojs
    功能:
    新增功能:
    日期:2022/6/2 17:12
"""
import xml.dom.minidom
 
 
dom = xml.dom.minidom.parse('web.xml')
root = dom.documentElement
bond_list = root.getElementsByTagName('filter-name')
 
print(bond_list[0].firstChild.data)

運行結(jié)果:

批量提取某個標簽值,并將其寫入文本

# coding=utf-8
"""
    作者:gaojs
    功能:
    新增功能:
    日期:2022/6/2 17:12
"""
import xml.dom.minidom
 
 
dom = xml.dom.minidom.parse('web.xml')
root = dom.documentElement
filter_list = root.getElementsByTagName('filter-name')
 
# print(filter_list[0].firstChild.data)
 
for bond in filter_list:
    s = bond.firstChild.data
    print(s)
    with open('filter_result.txt', 'a') as fin:
        fin.write(s + '\n')

文件結(jié)果:

第二種:正則提取xml指定內(nèi)容方法

with open('web.xml', mode='r') as fin:
    test = fin.read()
    result = re.findall('<filter-name>(.*?)</filter-name>', test)
    for key in result:
        print(key)
        with open('array/filter_result.txt', 'a') as f:
            f.write(key + '\n')

結(jié)果:

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 對于Python深淺拷貝的理解

    對于Python深淺拷貝的理解

    這篇文章主要介紹了對于Python深淺拷貝的理解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • 代碼講解Python對Windows服務(wù)進行監(jiān)控

    代碼講解Python對Windows服務(wù)進行監(jiān)控

    本篇文章給大家分享了通過Python對Windows服務(wù)進行監(jiān)控的實例代碼,對此有興趣的朋友可以學(xué)習(xí)參考下。
    2018-02-02
  • 詳談Python中列表list,元祖tuple和numpy中的array區(qū)別

    詳談Python中列表list,元祖tuple和numpy中的array區(qū)別

    下面小編就為大家分享一篇詳談Python中列表list,元祖tuple和numpy中的array區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 詳解OpenCV執(zhí)行連通分量標記的方法和分析

    詳解OpenCV執(zhí)行連通分量標記的方法和分析

    在本教程中,您將學(xué)習(xí)如何使用?OpenCV?執(zhí)行連通分量標記和分析。具體來說,我們將重點介紹?OpenCV?最常用的連通分量標記函數(shù):cv2.connectedComponentsWithStats,感興趣的可以了解一下
    2022-08-08
  • 使用Python完成15位18位身份證的互轉(zhuǎn)功能

    使用Python完成15位18位身份證的互轉(zhuǎn)功能

    這篇文章主要介紹了使用Python完成15位18位身份證的互轉(zhuǎn)功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-11-11
  • 九個Python列表生成式高頻面試題匯總

    九個Python列表生成式高頻面試題匯總

    本文為大家整理了九個Python列表生成式的面試題(從簡單到困難排序),可以幫助大家提高列表生成式的理解水平,感興趣的小伙伴可以學(xué)習(xí)一下
    2022-05-05
  • python的函數(shù)最詳解

    python的函數(shù)最詳解

    這篇文章主要介紹了詳解python中各種的函數(shù),是Python入門中的基礎(chǔ)知識,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-10-10
  • Python中matplotlib中文亂碼解決辦法

    Python中matplotlib中文亂碼解決辦法

    Matplotlib是Python的一個很好的繪圖包,但是其本身并不支持中文(貌似其默認配置中沒有中文字體),所以如果繪圖中出現(xiàn)了中文,就會出現(xiàn)亂碼
    2017-05-05
  • python數(shù)據(jù)分析之用sklearn預(yù)測糖尿病

    python數(shù)據(jù)分析之用sklearn預(yù)測糖尿病

    這篇文章主要介紹了python數(shù)據(jù)分析之用sklearn預(yù)測糖尿病,文中有非常詳細的代碼示例,對正在學(xué)習(xí)python數(shù)據(jù)分析的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-04-04
  • python實現(xiàn)excel和csv中的vlookup函數(shù)示例代碼

    python實現(xiàn)excel和csv中的vlookup函數(shù)示例代碼

    這篇文章主要介紹了python實現(xiàn)excel和csv中的vlookup函數(shù),介紹如何使用python在excel和csv里實現(xiàn)vlookup函數(shù)的功能,首先需要簡單了解一下python如何操作excel,需要的朋友可以參考下
    2023-01-01

最新評論