Web サイトのエンコーディングに問題があります。Web サイトをスクレイピングするプログラムを作成しましたが、読み取ったコンテンツのエンコーディングを変更することに成功しませんでした。私のコードは次のとおりです。
import sys,os,glob,re,datetime,optparse
import urllib2
from BSXPath import BSXPathEvaluator,XPathResult
#import BeautifulSoup
#from utility import *
sTargetEncoding = "utf-8"
page_to_process = "http://www.xxxx.com"
req = urllib2.urlopen(page_to_process)
content = req.read()
encoding=req.headers['content-type'].split('charset=')[-1]
print encoding
ucontent = unicode(content, encoding).encode(sTargetEncoding)
#ucontent = content.decode(encoding).encode(sTargetEncoding)
#ucontent = content
document = BSXPathEvaluator(ucontent)
print "ORIGINAL ENCODING: " + document.originalEncoding
私は外部ライブラリ(BSXPath BeautifulSoapの拡張)を使用し、document.originalEncodingは、変更しようとしたutf-8エンコーディングではなく、Webサイトのエンコーディングを出力します。誰か提案がありますか?
ありがとう