Extracting text from XML using python

前端 未结 6 1910
野的像风
野的像风 2020-12-09 11:48

I have this example xml file


  Chapter 1
  Welcome to Chapter 1



        
6条回答
  •  青春惊慌失措
    2020-12-09 12:33

    For working (navigating, searching, and modifying) with XML or HTML data, I found Beautiful library very useful. For installation problem or detailed information, click on link.

    To find Attribute (tag) or multi-attribute values:

    from bs4 import BeautifulSoup
    data = """
    
    
    
    
    PALS SOCIETY OF 
    CANADA
    13479 77 AVE
    
    """
    
    soup = BeautifulSoup(data, "lxml")
    page_tag = soup.find_all('page')
    details_tag = page_tag[0].find_all('text')
    details_tag_count = len(details_tag)
    for iter_text in range(details_tag_count):
        print("Text : ", details_tag[iter_text].text)
        print("Left tag : ", details_tag[iter_text].get("left"))
    

    Output:

    Text :  PALS SOCIETY OF CANADA
    Left tag :  135
    Text :  13479 77 AVE
    Left tag :  None
    

提交回复
热议问题