2016-03-21 27 views
3

Programı, (http://upodn.com/phon.php) kullanarak BeautifulSoup kullanarak herhangi bir sözcük göndermeye çalışıyorum ve sonra sonucu yazdırayım. həlo ama benim script kullanarak "Merhaba" kelimesini gönderdiğinizde bunun sonucudur: örneğin i (http://upodn.com/phon.php) web sitesine "Merhaba" kelimesini gönderdiğinizde sonucudur həloUnicode nasıl sonuçlanır BeautifulSoup

i sonucunu yazdırabilirsiniz nasıl onun web sitesinde göründüğü gibi =>həlo?

Senaryo:

çıkış
# -*- 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] 

: Sen BeautifulSoup, BeautifulSoup 3. güncel sürümü kesinlikle eski sürümünü kullanıyorsanız

həlo 
[Finished in 2.7s] 
+1

Her şeyden önce, BeautifulSoup'un eski sürümünü kullanıyorsunuz; mevcut sürüm paketi ve modülü 'bs4' –

cevap

2

, BeautifulSoup 4 PyPI içinde beautifulsoup4 denilen ve en iyi sahiptir seviye paketi bs4.

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 

BeautifulSoup3 kullanan yeni kod yazarken hiçbir anlamı yoktur, böylece artık geçiş yapmalıdır: BeautifulSoup 4 bu html varlıkları kodunu çözer.

+0

Bu mükemmel çalışır, teşekkürler :) –