2
一部のHTMLで特定の情報を見つけるために、次のコードを実行しています。しかし、私は解決できないエンコード/デコードの問題を抱えています。urllibリクエストからユニコードを取得する
import urllib
req = urllib.urlopen('http://securities.stanford.edu/1046/AAI00_01/')
html = req.read()
type(html)
# <type 'str'>
html.upper().find('HTML')
# -1
print html[0:20]
# ??<HTML><HE
html[0:10]
# '\xff\xfe<\x00H\x00T\x00M\x00'
req.headers['content-type']
# 'text/html'
html = html.encode('utf-8')
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
この問題の解決方法を教えてください。私がする必要があるのは、.findと正規表現を使用してページから情報を取り出すことだけです。
私はMac OSXを使用していて、Python 2.6.1をTerminal内から実行しています。