Get div content by id using BeautifulSoup

I am using python2.7.6, urllib2 and BeautifulSoup

to extract html from a website and save in a variable.

How can I only show the contents of the html div with id using beautifulsoup?

 <div id='theDiv'> <p>div content</p> <p>div stuff</p> <p>div thing</p> 

will be

 <p>div content</p> <p>div stuff</p> <p>div thing</p> 
+6
source share
1 answer

Attach the elements of the div .contents tag:

 from bs4 import BeautifulSoup data = """ <div id='theDiv'> <p>div content</p> <p>div stuff</p> <p>div thing</p> </div> """ soup = BeautifulSoup(data) div = soup.find('div', id='theDiv') print ''.join(map(str, div.contents)) 

Print

 <p>div content</p> <p>div stuff</p> <p>div thing</p> 
+11
source

Source: https://habr.com/ru/post/974667/


All Articles