Saturday, November 9, 2013

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"})


Priya angel said...

nice blog thank you sharing python Online Training

Priya angel said...

nice its a wonderfull blog python Online Training

Praveen said...

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

Get function name programaticaly - Python

This little piece of code will help you to get the function name programatically. This is very helpful when you are implementing the debug...