BeautifulSoup 无法解析带有选项的 html 页面html5lib
,但使用该选项可以正常工作html.parser
。根据文档,html5lib
应该比html.parser
,那么为什么我在使用它来解析 html 页面时遇到乱码?
下面是一个可执行的小例子。(修改html5lib
with后html.parser
,中文输出正常。)
#_*_coding:utf-8_*_
import requests
from bs4 import BeautifulSoup
ss = requests.Session()
res = ss.get("http://tech.qq.com/a/20151225/050487.htm")
html = res.content.decode("GBK").encode("utf-8")
soup = BeautifulSoup(html, 'html5lib')
print str(soup)[0:800] # where you can see if the html is parsed normally or not