Home  >  Q&A  >  body text

python - ulipad爬网页时中文为何是16进制?

#! /usr/bin/env python
#coding=utf-8
import urllib

url = "http://www.tust.edu.cn"
content = urllib.urlopen(url).read()
print content

python初学者,也是编程初学者。
上面是代码,用python2.7编译的时候网页上的中文可以正常显示,但是用ulipad编译的时候,网页中的文字就是以16进制的方式显示的。请问这个是什么问题?

我搜索的时候,看到有的回答说是在代码后面加上encode

然而我在content后面接encode('utf-8')或者encode('gb2312')的时候都会提示报错

ascii codec can't decode byte 0Xef in position 0:ordinal not in range

我查了一下,找到了下面这个网址

http://blog.csdn.net/qian_f/a...

好像是说是字符编码不统一的问题。紧接着我查了一下read返回的好像就是byte string呀....

求各位大神解答...

怪我咯怪我咯2719 days ago481

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-04-18 10:31:31

    My personal understanding is that the default encoding of read() cannot parse the web page content. Check the information and try to specify the encoding in the brackets of read. There should be no need to use encode

    reply
    0
  • 阿神

    阿神2017-04-18 10:31:31

    unicode.encode(content,'utf-8');

    reply
    0
  • Cancelreply