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

python操作xml文件示例

 更新時(shí)間:2014年04月07日 08:14:28   作者:  
這篇文章主要介紹了python操作xml文件示例,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

def get_seed_data(filename):
dom = minidom.parse(filename)
root = dom.documentElement
system_nodes = root.getElementsByTagName("system")
k = 0
seed_list = []
for system_node in system_nodes:
    #print system_node.nodeName+' id='+system_node.getAttribute('id')
    system_id = system_node.getAttribute("id")
    system_name = system_node.getAttribute("name")
    #print 'system_name:%s'%system_name
    section_nodes = system_node.getElementsByTagName("section")
    for section_node in section_nodes:
            section_id = section_node.getAttribute('id')
            section_name = section_node.getAttribute('name')
            #print ' '+section_node.nodeName+' id='+section_id+' name='+section_name
            crawl_cycle_node = section_node.getElementsByTagName("crawl_cycle")
            crawl_cycle = crawl_cycle_node[0].childNodes[0].nodeValue
            #print '  '+crawl_cycle_node[0].nodeName+'='+crawl_cycle
            seed_nodes = section_node.getElementsByTagName('seed')
            for seed_node in seed_nodes:
                seed = {}
                seed['crawl_cycle'] = crawl_cycle
                seed['system_id'] = int(system_id)
                seed['system_name'] = system_name
                seed['section_id'] = int(section_id)
                seed['section_name'] = section_name
                seed_id = seed_node.getAttribute('id')
                seed['seed_id'] = int(seed_id)
                #print '  '+seed_node.nodeName+' '+'id='+seed_id
                userblog_url_node = seed_node.getElementsByTagName('userblog_url')
                userblog_url = userblog_url_node[0].childNodes[0].nodeValue
                seed['userblog_url'] = userblog_url
                #print '   '+'userblog_url'+' '+userblog_url
                print '-------------------------------------------'
                print 'system_id:%d' % seed['system_id']
                print 'system_name:%s'%seed['system_name']
                print ' section_id:%d' % seed['section_id']
                print ' section_name:%s' % seed['section_name']
                print '  seed_id:%d' %seed['seed_id']
                print '  userblog_url:%s' %seed['userblog_url']
                print '========================='
                seed_list.append(seed)
                print seed_list[k]
                k += 1
                os.system('pause')
return seed_list

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8" ?>
<seeds>
 <system id="1" name="新浪">
  <section id="1" name="娛樂">
   <crawl_cycle> </crawl_cycle>
   <seed id="1">
    <userblog_url>http://aaa.com.cn/loveissuuny</userblog_url>
   </seed>
   <seed id="2">
    <userblog_url>http://aaa.com.cn/loveissuuny</userblog_url>
   </seed>
   <seed id="3">
    <userblog_url>http://aaa.com.cn/sanxiazaixian</userblog_url>
   </seed>
  </section>
  <section id="2" name="讀書">
   <crawl_cycle> </crawl_cycle>
   <seed id="11">
    <userblog_url>http://aaa.com.cn/twocold</userblog_url>
   </seed>
   <seed id="12">
    <userblog_url>http://aaa.com.cn/u/1233526741</userblog_url>
   </seed>
  </section>
 </system>
</seeds>

相關(guān)文章

  • Python之tkinter進(jìn)度條Progressbar用法解讀

    Python之tkinter進(jìn)度條Progressbar用法解讀

    這篇文章主要介紹了Python之tkinter進(jìn)度條Progressbar用法解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Python函數(shù)中的作用域規(guī)則詳解

    Python函數(shù)中的作用域規(guī)則詳解

    這篇文章主要為大家詳細(xì)介紹了Python的作用域規(guī)則,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • PyCharm取消波浪線、下劃線和中劃線的實(shí)現(xiàn)

    PyCharm取消波浪線、下劃線和中劃線的實(shí)現(xiàn)

    這篇文章主要介紹了PyCharm取消波浪線、下劃線和中劃線的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • python+opencv實(shí)現(xiàn)高斯平滑濾波

    python+opencv實(shí)現(xiàn)高斯平滑濾波

    這篇文章主要為大家詳細(xì)介紹了python+opencv實(shí)現(xiàn)高斯平滑濾波,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 在Python中預(yù)先初始化列表內(nèi)容和長(zhǎng)度的實(shí)現(xiàn)

    在Python中預(yù)先初始化列表內(nèi)容和長(zhǎng)度的實(shí)現(xiàn)

    今天小編就為大家分享一篇在Python中預(yù)先初始化列表內(nèi)容和長(zhǎng)度的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python使用pandas導(dǎo)入csv文件內(nèi)容的示例代碼

    Python使用pandas導(dǎo)入csv文件內(nèi)容的示例代碼

    這篇文章主要介紹了Python使用pandas導(dǎo)入csv文件內(nèi)容,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • Python 40行代碼實(shí)現(xiàn)人臉識(shí)別功能

    Python 40行代碼實(shí)現(xiàn)人臉識(shí)別功能

    很多人都認(rèn)為人臉識(shí)別是一項(xiàng)非常難以實(shí)現(xiàn)的工作,看到名字就害怕,然后心懷忐忑到網(wǎng)上一搜,看到網(wǎng)上N頁(yè)的教程立馬就放棄了。其實(shí)如果你不是非要深究其中的原理,只是要實(shí)現(xiàn)這一工作的話,人臉識(shí)別也沒那么難。今天我們就來看看如何在40行代碼以內(nèi)簡(jiǎn)單地實(shí)現(xiàn)人臉識(shí)別。
    2017-04-04
  • python中查看.db文件中表格的名字及表格中的字段操作

    python中查看.db文件中表格的名字及表格中的字段操作

    這篇文章主要介紹了python中查看.db文件中表格的名字及表格中的字段操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 對(duì)pycharm 修改程序運(yùn)行所需內(nèi)存詳解

    對(duì)pycharm 修改程序運(yùn)行所需內(nèi)存詳解

    今天小編就為大家分享一篇對(duì)pycharm 修改程序運(yùn)行所需內(nèi)存詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python3.6簡(jiǎn)單反射操作示例

    Python3.6簡(jiǎn)單反射操作示例

    這篇文章主要介紹了Python3.6簡(jiǎn)單反射操作,結(jié)合實(shí)例形式分析了Python3反射的概念、原理、相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-06-06

最新評(píng)論