好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

pythonBeautifulSoup设置页面编码的方法

在用BeautifulSoup进行抓取页面的时候,会各种各样的编码错误。
可以通过在beautifulsoup中指定字符编码,解决问题。

代码如下:


import urllib2
from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen('http://HdhCmsTest163测试数据');
soup = BeautifulSoup(page,from_encoding="gb2312")

print soup.originalEncoding
print soup.prettify()


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

查看更多关于pythonBeautifulSoup设置页面编码的方法的详细内容...

  阅读:50次