How to convert BeautifulSoup.ResultSet to string

So, I analyzed the html page with a .findAll(BeautifulSoup) variable named result. If I type resultin the Python shell and press Enter, I will see plain text as expected, but since I would like to process this result as a string object, I noticed that it str(result)returns garbage, like this sample:

\xd1\x87\xd0\xb8\xd0\xbb\xd0\xbd\xd0\xb8\xd1\x86\xd0\xb0</a><br />\n<hr />\n</div>

HTML page source utf-8encoded

How can I handle this?


The code is basically this, in case it matters:

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(urllib.open(url).read())
result = soup.findAll(something)

Python 2.7

+5
source share
4 answers

Python 2.6.7 BeautifulSoup. version 3.2.0

This worked for me:

unicode.join(u'\n',map(unicode,result))

, result - BeautifulSoup.ResultSet, , -, python

+10
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(urllib.open(url).read())
#findAll should get multiple parsed result
result = soup.findAll(something)
#then iterate result
for line in result:
    #get str value from each line,replace charset with utf-8 or other charset you need
    print line.__str__('charset')

BTW: BeautifulSoup beautifulsoup-3.2.1

+3

, UTF-8. Unicode.

0

:

unicodedata.normalize('NFKC', p.decode()).encode('ascii','ignore')

Unicode .
originalEncoding .
unicode python, ( )

0

All Articles