I want to use BeautifulSoup to get the text from an HTML string. While get_text()'s separator argument is nice, I would like to use different separators for different tags (or not use any at all for others).
As an example, consider the HTML:
<p>This is some paragraph text. With a <a href="example.com">link</a>.</p>
<div>This is another paragraph.</div>
Dummy code:
from bs4 import BeautifulSoup
string = '<p>This is some paragraph text. With a <a href="example.com">link</a>.</p>\n<div>This is another paragraph.</div>'
soup = BeautifulSoup(string)
text = soup.get_text('\n', strip=True)
print(text)
Using get_text('\n') outputs
This is some paragraph text. With a
link
.
This is another paragraph.
But the desired output would be
This is some paragraph text. With a link.
This is another paragraph.
Is there a way to use get_text() and use the '\n' string as a separator for most tags and no separators for "inline" tags like <a> or <b>?
Note that the HTML I am parsing is not consistent so I can't use a function that corrects this behavior afterwards.
EDIT:
The reason for using a separator as an argument in get_text() is that the input is not guaranteed to have a newline between the two paragraphs.
If the example HTML was
<p>This is some paragraph text. With a <a href="example.com">link</a>.</p><div>This is another paragraph.</div>
the output still has to have the contents of <p> tags separated somehow.
EDIT 2: Added different tags to the examples.