简体   繁体   中英

how to Unicode result in BeautifulSoup

I try to make program to submit any word to ( http://upodn.com/phon.php ) using BeautifulSoup then print the result. for example when i submit the "hello" word to ( http://upodn.com/phon.php ) website the result is: həlo but when i submit the "hello" word using my script it the result is: həlo

How i can print the result as its appear in the website => həlo ?

Script:

#  -*- coding: utf-8 -*-

import mechanize
import cookielib
from BeautifulSoup import BeautifulSoup
import html2text

br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1'), ('Content-type', 'text/html; charset=utf-8')]
br.open('http://upodn.com/phon.php')
br.select_form(nr=0)
br.form['intext'] = 'hello'
br.submit()
data = br.response().read()
soup = BeautifulSoup(data)
# print soup
table = soup.find('table', {'rules': 'cols'})
result = []
for row in table.findAll("font"):
    d = row.text
    result.append(d)
print result[1]

output:

həlo
[Finished in 2.7s]

You're using absolutely obsolete version of BeautifulSoup, BeautifulSoup 3. The current version, BeautifulSoup 4 is called beautifulsoup4 in PyPI and has top-level package bs4 . BeautifulSoup 4 decodes these html entities:

Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
>>> print(BeautifulSoup('<b>h&#x0259;lo</b>').find('b').text)
həlo

There is no point in writing new code that uses BeautifulSoup3, so you should switch now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM