手机
当前位置:查字典教程网 >脚本专栏 >python >python BeautifulSoup设置页面编码的方法
python BeautifulSoup设置页面编码的方法
摘要:在用BeautifulSoup进行抓取页面的时候,会各种各样的编码错误。可以通过在beautifulsoup中指定字符编码,解决问题。复制代...

在用BeautifulSoup进行抓取页面的时候,会各种各样的编码错误。

可以通过在beautifulsoup中指定字符编码,解决问题。

复制代码 代码如下:

import urllib2

from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen('http://www.163.com');

soup = BeautifulSoup(page,from_encoding="gb2312")

print soup.originalEncoding

print soup.prettify()

红色部分表示需要注意的地方。在BeautifulSoup构造器中传入fromEncoding参数即可解决乱码问题,当然具体参数值是什么就要看你获取页面的编码是什么

【python BeautifulSoup设置页面编码的方法】相关文章:

Python 条件判断的缩写方法

python3访问sina首页中文的处理方法

python实现问号表达式(?)的方法

Python BeautifulSoup中文乱码问题的2种解决方法

python sqlobject(mysql)中文乱码解决方法

Python中使用中文的方法

python读取注册表中值的方法

python操作日期和时间的方法

python生成器的使用方法

Python获取当前时间的方法

精品推荐
分类导航