Проблема с очисткой данных с помощью BeautifulSoup - PullRequest
0 голосов
/ 01 июля 2010

Я написал следующий пробный код для получения названия законодательных актов из Европейского парламента.

import urllib2
from BeautifulSoup import BeautifulSoup

search_url = "http://www.europarl.europa.eu/sides/getDoc.do?type=REPORT&mode=XML&reference=A7-2010-%.4d&language=EN"

for number in xrange(1,10):   
    url = search_url % number
    page = urllib2.urlopen(url).read()
    soup = BeautifulSoup(page)
    title = soup.findAll("title")
    print title

Однако, когда я запускаю его, я получаю следующую ошибку:

Traceback (most recent call last):
  File "<stdin>", line 20, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 70: ordinal not in range(128)

Я сузил его до того, что BeautifulSoup не смог прочитать четвертый документ в цикле.Может кто-нибудь объяснить мне, что я делаю неправильно?

С уважением

Томас

Ответы [ 3 ]

4 голосов
/ 01 июля 2010

BeautifulSoup работает в Юникоде, поэтому он не несет ответственности за ошибку декодирования . Скорее всего, ваша проблема связана с оператором print - ваш стандартный вывод выглядит как ascii (т. Е. sys.stdout.encoding = 'ascii' или отсутствует), и поэтому вы действительно получите такие ошибки, если попытаетесь напечатать строку, содержащую не-ascii символы .

Какая у вас ОС? Как настроен ваш консольный терминал АКА (например, если в Windows какая «кодовая страница»)? Вы установили в среде PYTHONIOENCODING управление sys.stdout.encoding или просто надеетесь, что кодировка будет получена автоматически?

На моем Mac, где кодировка равна , правильно определена, запуск вашего кода (за исключением для печати номера вместе с каждым заголовком, для ясности) работает нормально и показывает:

$ python ebs.py 
1 [<title>REPORT Report on the proposal for a Council regulation temporarily suspending autonomous Common Customs Tariff duties on imports of certain industrial products into the autonomous regions of Madeira and the Azores - A7-0001/2010</title>]
2 [<title>REPORT Report on the proposal for a Council directive concerning mutual assistance for the recovery of claims relating to taxes, duties and other measures - A7-0002/2010</title>]
3 [<title>REPORT Report on the proposal for a regulation of the European Parliament and of the Council amending Council Regulation (EC) No 1085/2006 of 17 July 2006 establishing an Instrument for Pre-Accession Assistance (IPA) - A7-0003/2010</title>]
4 [<title>REPORT on equality between women and men in the European Union – 2009 - A7-0004/2010</title>]
5 [<title>REPORT Report on the proposal for a Council decision on the conclusion by the European Community of the Convention on the International Recovery of Child Support and Other Forms of Family Maintenance - A7-0005/2010</title>]
6 [<title>REPORT on the proposal for a Council directive on administrative cooperation in the field of taxation - A7-0006/2010</title>]
7 [<title>REPORT Report on promoting good governance in tax matters - A7-0007/2010</title>]
8 [<title>REPORT Report on the proposal for a Council Directive amending Directive 2006/112/EC as regards an optional and temporary application of the reverse charge mechanism in relation to supplies of certain goods and services susceptible to fraud - A7-0008/2010</title>]
9 [<title>REPORT Recommendation on the proposal for a Council decision concerning the conclusion, on behalf of the European Community, of the Additional Protocol to the Cooperation Agreement for the Protection of the Coasts and Waters of the North-East Atlantic against Pollution - A7-0009/2010</title>]
$ 
1 голос
/ 01 июля 2010

Замена

print title

на

for t in title:
    print(t)

или

print('\n'.join(t.string for t in title))

работает.Я не совсем уверен, почему print <somelist> иногда работает, а иногда нет.

0 голосов
/ 01 июля 2010

Если вы хотите напечатать заголовки в файле, вам нужно указать некоторую кодировку, которая может представлять не-ascii char, utf8 должен работать нормально. Для этого нужно добавить:

out = codecs.open('titles.txt', 'w', 'utf8')

вверху скрипта

и распечатать в файл:

print >> out, title
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...