2012-05-15 3 views
1

Мой скрипт работает, когда я загружаю английскую Библию. но дает мне ошибку ascii, когда я загружаю иностранную Библию.python beautiful soup ascii error

питон

from BeautifulSoup import BeautifulSoup, Tag, NavigableString 
import lxml.html as html 
import urlparse 
import os, sys 
import urllib2 
import re 
print ("downloading and converting Bibles to Aurora...") 
root = html.parse(open('links.html')) 
for link in root.findall('//a'): 
    url = link.get('href') 
    name = urlparse.urlparse(url).path.split('/')[-1] 
    namesave = '%s.html' % '.'.join(name.split('.')[:-1]) 
    chnum = name.split('.')[-2] 
    dirname = urlparse.urlparse(url).path.split('.')[-1] 
    try: 
     f = urllib2.urlopen(url) 
    except urllib2.URLError: 
     print "Bad URL or timeout" 
     continue 
    s = f.read() 
    if (os.path.isdir(dirname) == 0): 
    os.mkdir(dirname) 
    soup = BeautifulSoup(s) 
    thearticle = soup.html.body.article 
    bookname = thearticle['data-book-human'] 
    soup.html.replaceWith('<html>'+str(bookname)+'</html>') 
    converted = str(soup) 
    full_path = os.path.join(dirname, namesave) 
    open(full_path, 'wb').write(converted) 
    print(name) 
print("DOWNLOADS AND CONVERSIONS COMPLETE!") 

links.html, который работает

<a href="http://www.youversion.com/bible/john.6.ceb">http://www.youversion.com/bible/john.6.ceb</a> 

links.html, что дает ошибку

<a href="http://www.youversion.com/bible/john.6.nav">http://www.youversion.com/bible/john.6.nav</a> 

ошибку

File "test.py", line 32, in <module> 
    soup.html.replaceWith('<html>'+str(bookname)+'</html>') 
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128) 

ответ

2

Я видел подобную ошибку раньше, может быть и такой же. Не могу точно вспомнить.

Try:

BeautifulSoup(s, convertEntities=BeautifulSoup.HTML_ENTITIES) 

Или попытаться заставить Юникода:

soup.html.replaceWith(u'<html>'+unicode(bookname)+u'</html>') 
+0

насколько я могу сказать, что это решение работает. будет отмечен как полный :) – Blainer