Python: Get div content using BeautifulSoup

I am going to show you an example that will parse a div content from the given html. Lets consider the python variable content holds the html source in it.


soup = BeautifulSoup(content)
div_content = soup.find("div", {"class": "product"})
print div_content
print str(div_content)


In this example, the code is basically parsing the div tags which are only having the class=product. You can add more attributes like id=... and so on. For example,

div_content = soup.find("div", {"class": "product", "id": "myDiv"})



Comments

Praveen said…
you can use getText() method
-----------------
_soup = BeautifulSoup(htmlData,'lxml')
try:
msgDiv = _soup.find_all('div',{"id":"msgDiv"})
for obj in msgDiv:
print("obj.getText() : ",obj.getText())
return obj.getText()
except:
return False
-----------------