我的网站编码有问题。我制作了一个程序来抓取一个网站,但我没有成功改变阅读内容的编码。我的代码是:
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 编码。有人有什么建议吗?
谢谢