попробуйте это:
со вторым примером:
import requests
from bs4 import BeautifulSoup
data=dict()
html_page = """<div dir="ltr">Can i buy a phone?<br clear="all">
<div><br>-- <br>
<div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">
<div dir="ltr"><span>
<div dir="ltr"><span style="color:rgb(136,136,136)"></span>
<div>
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div> Some Important Divs</div>
</div>
</div>
</div>
</div>
</div>
</span></div>
</div>
</div>
</div>"""
soup = BeautifulSoup(html_page, 'html.parser')
text = soup.find_all(text=True)
output = ''
blacklist = [
#'[document]',
#'noscript',
#'header',
'html',
#'meta',
#'head',
#'input',
#'script',
# there may be more elements you don't want, such as "style", etc.
]
for t in text:
if t.parent.name not in blacklist:
output += '{} '.format(t)
if "--" in output:
res=output.replace("\n","").split("--")
else:
res=output.replace("\n","").split("Best Regards ")
data["body"]=res[0]
data["signature"]=res[1].strip()
print(data)
выход:
{'body': 'Can i buy a phone? ', 'signature': 'Some Important Divs'}
с первым:
import requests
from bs4 import BeautifulSoup
data=dict()
html_page = """can i buy a laptop<br><br>-- <br>
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div dir="ltr">
<p style="color:rgb(0,0,0);font-family:times;font-size:medium">
Some important Text/ Email Signature
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div><br>"""
soup = BeautifulSoup(html_page, 'html.parser')
text = soup.find_all(text=True)
output = ''
blacklist = [
#'[document]',
#'noscript',
#'header',
'html',
#'meta',
#'head',
#'input',
#'script',
# there may be more elements you don't want, such as "style", etc.
]
for t in text:
if t.parent.name not in blacklist:
output += '{} '.format(t)
if "--" in output:
res=output.replace("\n","").split("--")
else:
res=output.replace("\n","").split("Best Regards ")
data["body"]=res[0]
data["signature"]=res[1].strip()
print(data)
выход: * +1010 *
{'body': 'can i buy a laptop ', 'signature': 'Some important Text/ Email Signature'}