This article describes how to operate xml files in python. For more information, see
The code is as follows:
Def get_seed_data (filename ):
Dom = minidom. parse (filename)
Root = dom.doc umentElement
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
The code is as follows:
Http://aaa.com.cn/loveissuuny </userblog_url>
Http://aaa.com.cn/loveissuuny </userblog_url>
Http://aaa.com.cn/sanxiazaixian </userblog_url>
Http://aaa.com.cn/twocold </userblog_url>
Http://aaa.com.cn/u/1233526741 </userblog_url>