Skip to content

Instantly share code, notes, and snippets.

@fallingfree
Created January 10, 2013 08:51
Show Gist options
  • Save fallingfree/4500564 to your computer and use it in GitHub Desktop.
Save fallingfree/4500564 to your computer and use it in GitHub Desktop.
解决 PyQuery 中文乱码的方法
# -*- coding: utf-8 -*-
# 2.如遇到 UnicodeEncodeError: 'ascii' codec can't encode characters in position 22-25: ordinal not in range(128)
# 增加如下3行
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from pyquery import PyQuery as pq
# 1.把11行换为12行, 重点是要把读出的内容转成unicode再存为PyQuery对象
# input_file = open(file).read()
input_file = unicode(open(file).read(), 'utf-8')
source = pq(input_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment