saurabh
saurabh

Reputation: 47

Extract the text element from html div class through beautiful soup python

I want to get the text I want mention in here within the a class tag

<a class="_2cLu-l" title="Realme 5 (Crystal Blue, 32 GB)" target="_blank" rel="noopener noreferrer" href="/realme-5-crystal-blue-32-gb/p/itmfj9twbwfznhyk?pid=MOBFJ9TWENA2FSGX&amp;lid=LSTMOBFJ9TWENA2FSGXSV98FT&amp;marketplace=FLIPKART&amp;srno=s_1_3&amp;otracker=search&amp;otracker1=search&amp;fm=organic&amp;iid=ddea8cef-acd0-4f05-a786-608404d850de.MOBFJ9TWENA2FSGX.SEARCH&amp;ppt=sp&amp;ppn=sp&amp;ssid=xagdh1n18g0000001568504585590&amp;qH=f7a42fe7211f98ac">TEXT I WANT</a>

I have tried with the following statement

for a in soup.findAll('a',href=True, attrs={'class':'_2cLu-l'}):


  name=a.find('a', attrs={'class':'_2cLu-l'})
  print(name)
  name.text

Upvotes: 0

Views: 71

Answers (1)

Matthew Gaiser
Matthew Gaiser

Reputation: 4763

This code worked for me to solve your test case.

from bs4 import BeautifulSoup

soup = BeautifulSoup('your html',"html.parser")
a = soup.findAll('a',text=True, attrs={'class':'_2cLu-l'})
for item in a:
    print(item.text)

Upvotes: 1

Related Questions