How to find elements by class

后端 未结 17 1444
有刺的猬
有刺的猬 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:13

    Alternatively we can use lxml, it support xpath and very fast!

    from lxml import html, etree 
    
    attr = html.fromstring(html_text)#passing the raw html
    handles = attr.xpath('//div[@class="stylelistrow"]')#xpath exresssion to find that specific class
    
    for each in handles:
        print(etree.tostring(each))#printing the html as string
    

提交回复
热议问题