hc6538 发表于 2017-5-5 06:45:15

python BeautifulSoup 中文编码问题解决

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

import urllib2
from BeautifulSoup import BeautifulSoup
page = urllib2.urlopen('http://www.163.com');
soup = BeautifulSoup(page,from_encoding="gb18030")
print soup.originalEncoding
print soup.prettify()
  红色部分表示需要注意的地方。在BeautifulSoup构造器中传入fromEncoding参数即可解决乱码问题,当然具体参数值是什么就要看你获取页面的编码是什么
页: [1]
查看完整版本: python BeautifulSoup 中文编码问题解决