Domanda

I would like to search through child elements for specific attributes using BeautifulSoup, from what I can see using the below method each child is a string (child['value'] gives me "string indices must be integers"), which does not allow selection based on attributes or returning of those attributes, which incidently is what I need to do.

def get_value(container):
    html_file = open(html_path)
    html = html_file.read()
    soup = BeautifulSoup(html)
    values = {}
    container = soup.find(attrs={"name" : container})
    if (container.contents != []):
        for child in container.children:
            value = unicode(child['value']) # i would like to be able to search throught these children based on their attributes, and return one or more of their values
return value

Could probably get around this with a further child_soup Beautifulsoup(child) and then a find command but this seems really horrible, anyone got a better solution?

È stato utile?

Soluzione

container.children is a generator that provides Tag objects, so you can operate on them normally.

You also might want to try element.find_all(..., recursive=False) in order to look for an element's direct children with some traits.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top