How to find elements by class

后端 未结 17 1625
有刺的猬
有刺的猬 2020-11-22 08:33

I\'m having trouble parsing HTML elements with \"class\" attribute using Beautifulsoup. The code looks like this

soup = BeautifulSoup(sdata)
mydivs = soup.fi         


        
17条回答
  •  醉梦人生
    2020-11-22 09:11

    This works for me to access the class attribute (on beautifulsoup 4, contrary to what the documentation says). The KeyError comes a list being returned not a dictionary.

    for hit in soup.findAll(name='span'):
        print hit.contents[1]['class']
    

提交回复
热议问题