0
我在使用網站編碼時遇到問題。我製作了一個程序來抓取一個網站,但我沒有成功地改變收錄內容的編碼。我的代碼是:檢測並更改python中的網站編碼
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編碼,我試圖改變。 有沒有人建議?
感謝