O P
O P

Reputation: 2365

Python unicode string to string?

This gets the contents of the last div with class="title":

soup = BeautifulSoup(html)
title = soup.find_all('div', {'class' : 'title' })[-1].contents

When I print(title) it outputs:

[u'Harry Potter']

How can I get it to print just:

Harry Potter

I tried print(str(title)) but that didn't change anything.

Upvotes: 2

Views: 716

Answers (2)

Robert Ingrum
Robert Ingrum

Reputation: 234

If you'd rather convert the unicode instead of getting a string object as @alecxe mentioned, you'll have to encode it.

print(title.encode('utf-8')) should work. If you're still getting the square brackets, just do print(title[0].encode('utf-8'))

Upvotes: 2

alecxe
alecxe

Reputation: 473833

Just get the text using get_text():

title = soup.find_all('div', {'class' : 'title' })[-1].get_text()

Upvotes: 1

Related Questions