There does not exist innerHTML function in BeautifulSoup. It means we can not get html string in a html element as JavaScript. In this tutorial, we will introduce you how to build a innerHTML function in BeautifulSoup.
Here is the example code:
from bs4 import BeautifulSoup html_doc = '<div class="s-prose js-post-body" itemprop="text">test<p>I do nontire small program and it threw me off. </p><p>How do I just play a single audio file? </p></div>' soup = BeautifulSoup(html_doc, 'html.parser') eles = soup.find_all("div") print(eles) def innerHTML(html_tag): text = "" for c in html_tag.contents: text+=str(c) return text text = innerHTML(eles[0]) print(text)
As to html div element in this example, the inner html of it is:
test<p>I do nontire small program and it threw me off. </p><p>How do I just play a single audio file? </p>
Then we can use function innerHTML() to get it.
Run the example code, we will get:
test<p>I do nontire small program and it threw me off. </p><p>How do I just play a single audio file? </p>
It is same to innerHTML() function in JavaScript.