Anurag
Anurag

Reputation: 482

How to extract texts that are not in any html tags using beautifulsoup?

The email string:

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>

Out put required:

{
   body: "can i buy a laptop",
   Signature: "Some important Text/ Email Signature"
}

Another problem is, the email text is dynamic. It can be like this as well :

<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>

So can't really be sure by 'ltr' tag. Till now I have been extracting with ltr tag for the first part and for signature by gmail_signature.

    soup = BeautifulSoup(emailText, 'html.parser')
    mainbody = soup.find('div', {'dir': 'ltr'})
    if mainbody is not None:
        texts = [t for t in mainbody.contents if isinstance(t, NavigableString)]
        print('Mainbody: ', mainbody)
        print('Texts: ', texts)
        if len(texts) != 0:
            for idx,txt in enumerate(texts):
                allText += txt
                if idx != len(texts):
                    allText += "\n"    
    quotes = soup.find('div', {'class': 'gmail_quote'})
    if quotes is not None:
        for div in quotes:
            replies += " " + div.text
            # replies = replies.replace("\n", "")
            replies = replies.replace("\r", "")
            replies = re.sub(' +', ' ',replies)

Upvotes: 0

Views: 641

Answers (1)

Ghassen
Ghassen

Reputation: 794

try this: with the second example :

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)

output:

{'body': 'Can i buy a phone?  ', 'signature': 'Some Important Divs'}

with the first one:

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)

output:

{'body': 'can i buy a laptop ', 'signature': 'Some important Text/ Email Signature'}

Upvotes: 1

Related Questions