from bs4 import BeautifulSoup


def parse_html_list(html):
    """ Parses an html code containing a list of instagram profiles, which are described between <li> ... </li> tags.
        For every element of the list, it extracts profilename and html code (<li> ... </li>), stripping off the "Follow/Unfollow" button.
        
        Returns:
          a dictionary in the form  {key: "profilename", value = "<li> ..... </li>}
    """
    print("Parsing list... ", end="")
    profiles = dict()

    bs = BeautifulSoup(html, 'html.parser')#'python-lxml') #'html.parser'
    lst = bs.findAll('li')
    for l in lst:
        #picking up first element of generator. 
        #it's ok, since the list has only 1 direct son
        l1 = next(l.children) 
        l2 = list(l1.children)
        #l2 has 2 sons: the iage and text section, and the button section.
        #deleting the button element
        l2[1].extract()
        l3 = list(l2[0].children)
        # l3[0] contains profile's image
        # l3[1] contains "profile name" and "link" to open it
        #l3[1].a.get('href') #this gets me the profile's link, in the form '/goga_asana_london/'
        profilename = l3[1].a.text #profile       
        #str(l): object's html code         
        profiles[profilename] = str (l) #profiles.add(profilename) #if the return value were  a Set()

    print("ok")
    return profiles