Python operation XML File Sample _python

Source: Internet
Author: User

Copy Code code as follows:

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

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<seeds>
<system id= "1" name= "Sina" >
<section id= "1" name= "Entertainment" >
<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= "reading" >
<crawl_cycle> </crawl_cycle>
<seed id= "One" >
<userblog_url>http://aaa.com.cn/twocold</userblog_url>
</seed>
<seed id= ">"
<userblog_url>http://aaa.com.cn/u/1233526741</userblog_url>
</seed>
</section>
</system>
</seeds>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.