Extracting an attribute value with beautifulsoup

前端 未结 9 2061
故里飘歌
故里飘歌 2020-11-22 04:38

I am trying to extract the content of a single \"value\" attribute in a specific \"input\" tag on a webpage. I use the following code:

import urllib
f = urll         


        
9条回答
  •  天命终不由人
    2020-11-22 05:43

    In Python 3.x, simply use get(attr_name) on your tag object that you get using find_all:

    xmlData = None
    
    with open('conf//test1.xml', 'r') as xmlFile:
        xmlData = xmlFile.read()
    
    xmlDecoded = xmlData
    
    xmlSoup = BeautifulSoup(xmlData, 'html.parser')
    
    repElemList = xmlSoup.find_all('repeatingelement')
    
    for repElem in repElemList:
        print("Processing repElem...")
        repElemID = repElem.get('id')
        repElemName = repElem.get('name')
    
        print("Attribute id = %s" % repElemID)
        print("Attribute name = %s" % repElemName)
    

    against XML file conf//test1.xml that looks like:

    
    
        
            XYZ
        
        
        
    
    

    prints:

    Processing repElem...
    Attribute id = 11
    Attribute name = Joe
    Processing repElem...
    Attribute id = 12
    Attribute name = Mary
    

提交回复
热议问题