Taylor29
Taylor29

Reputation: 55

Python BeautifulSoup - Text Between Div

I am working on a webscraper project and can't get BeautifulSoup to give me the text between the Div. Below is my code. Any suggestions on how to get python to print just the "5x5" without the "Div to /Div" and without the whitespace?

source = requests.get('https://www.stor-it.com/self-storage/meridian-id-83646').text
soup = BeautifulSoup(source, 'lxml')
unit = soup.find('div', class_="unit-size")
print (unit)

This script returns the following:

<div class="unit-size">
                                    5x5                                 </div>

Upvotes: 1

Views: 225

Answers (3)

QHarr
QHarr

Reputation: 84465

Use a faster css class selector

from bs4 import BeautifulSoup
source= '''
<div class="unit-size">
                                    5x5                                 </div>
'''
soup = BeautifulSoup(source, 'lxml')
unit = soup.select('.unit-size')
print(unit[0].text.strip())

Upvotes: 0

Edeki Okoh
Edeki Okoh

Reputation: 1844

Change your print statement from print(unit) to print(unit.text)

Upvotes: 0

JimmyA
JimmyA

Reputation: 686

You can use text to retrieve the text, then strip to remove whitespace Try unit.text.strip()

Upvotes: 2

Related Questions