Answer a question

I am trying to scrape a webpage which has the following tag:

  <div style="text-align: center;">
            <img src="https://documents.google.com/" alt="" width="60" height="30" />
            <br />
            Pick me please.

        <p> Do not pick me please! </p>

        <br />
        <br />
    </div>

I want to scrape the "pick me please" string but don't want to scrape the "Do not pick me please!" string. Any idea how?

EDIT : I would appreciate a more generic solution, where I always wish to get the text under a particular tag, which is not inside any sub-tag

Answers

Edit

A more "generic" solution to find() the non empty text node in the div:

parent = soup.select_one('div')
parent.find(text=lambda text: text and text.strip(), recursive=False).strip()

To get the text node use previous_sibling and to avoid spaces,... strip() the result.

soup.select_one('div p').previous_sibling.strip()

or use get_text() and strip:

soup.select_one('div').get_text('|', strip=True).split('|')[0]

Minimal example

from bs4 import BeautifulSoup

html = '''
<div style="text-align: center;">
            <img src="https://documents.google.com/" alt="" width="60" height="30" />
            <br />
            Pick me please.

        <p> Do not pick me please! </p>

        <br />
        <br />
    </div>
'''
soup = BeautifulSoup(html, 'lxml')

soup.select_one('div p').previous_sibling.strip()

Output

Pick me please.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐