How to find elements by class

后端 未结 17 1649
有刺的猬
有刺的猬 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 08:51

    The following should work

    soup.find('span', attrs={'class':'totalcount'})
    

    replace 'totalcount' with your class name and 'span' with tag you are looking for. Also, if your class contains multiple names with space, just choose one and use.

    P.S. This finds the first element with given criteria. If you want to find all elements then replace 'find' with 'find_all'.

提交回复
热议问题