Example #1
0
f=open('test.xml')
xml = f.read()
soup = BeautifulSoup(xml)
for node in soup.findAll('node'):
    print("Node:"+node.string)
    try:
        node['attr1']
        print("Attr1:"+node['attr1'])
    except:
        print()

#Song xml 예제
f=open('song.xml',encoding='utf-8')
xml = f.read()
soup = BeautifulSoup(xml)
for nodes in soup.test('song'):
    for node in nodes:
        print(node.string)

#Alcohol xml 예제
f=open('alcohol.xml',encoding='utf-8')
xml = f.read()
soup = BeautifulSoup(xml,'lxml')
for nodes in soup.alcohol('cate1'):
    if nodes['tt']=="안주":
        print('Cate1:'+nodes['tt'])
        for node in nodes('cate2'):
            print('\tCate2:'+node['tt'])
            for item in node('item'):
                print('\t\t'+item.string)
Example #2
0
from bs4 import BeautifulSoup

f = open('test.xml')
xml = f.read()
soup = BeautifulSoup(xml, 'html.parser')
for node in soup.findAll('node'):
    print("Node : " + node.string)
    print("Attr1 : " + node['attr1'])

f = open('song.xml', encoding='utf-8')
xml = f.read()
soup = BeautifulSoup(xml, 'html.parser')
for nodes in soup.test('song'):
    for node in nodes:
        print(node.string)

f = open('alcohol.xml', encoding='utf-8')
xml = f.read()
soup = BeautifulSoup(xml, 'html.parser')
for nodes in soup.alcohol('cate1'):
    print('Cate1 :' + nodes['tt'])
    for node in nodes('cate2'):
        print('\tCate2 :' + node['tt'])
        for item in node('item'):
            print('\t\t' + item.string)

#안주만 받아오세요
f = open('alcohol.xml', encoding='utf-8')
xml = f.read()
soup = BeautifulSoup(xml, 'html.parser')
for nodes in soup.alcohol('cate1'):