0

私は、欧州議会から立法行為のタイトルを取得するために、次の裁判コードを作成しました。

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)

ループ内の4番目のドキュメントを読み取れないBeautifulSoupに絞り込みました。誰かが私が間違っていることを私に説明できますか?

よろしくお願いします

トーマス

4

3 に答える 3

4

BeautifulSoupはUnicodeで動作するため、そのデコードエラーの原因にはなりません。多くの場合、問題はprintステートメントにあります。標準の出力はASCII(つまり、sys.stdout.encoding = 'ascii'存在しない)であるように見えるため、ASCII以外の文字を含む文字列を印刷しようとすると、実際にそのようなエラーが発生します。

あなたのOSは何ですか?コンソールのAKA端末はどのように設定されていますか(たとえば、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>]
$ 
于 2010-07-01T14:16:37.300 に答える
1

交換

print title

for t in title:
    print(t)

また

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

動作します。なぜprint <somelist>うまくいくのか、うまくいかないのか、完全にはわかりません。

于 2010-07-01T14:16:01.237 に答える
0

タイトルをファイルに出力する場合は、非ASCII文字を表すことができるエンコードを指定する必要があります。utf8は正常に機能するはずです。これを行うには、次を追加する必要があります。

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

スクリプトの上部に

そしてファイルに印刷します:

print >> out, title
于 2010-07-01T16:22:19.780 に答える