首页 > 引起BeautifulSoup报错的原因是?

引起BeautifulSoup报错的原因是?

交互式运行的代码:

[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.


import requests
from bs4 import BeautifulSoup

s=requests.Session()
r=s.get('https://www.tumblr.com/login')
htmldoc=r.text
test=BeautifulSoup(htmldoc)

此时就会抛出一个 异常,

UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html5lib"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

To get rid of this warning, change this:

 BeautifulSoup([your markup])

to this:

 BeautifulSoup([your markup], "html5lib")

  markup_type=markup_type))

此时再运行一次最后一条语句,就正常了。
此问题是因为什么引起的?
为什么再运行一次就正常了?
重进交互式运行,到倒数第二步时,按照异常提示中的写法重写,依然会抛出异常。


首先这不是错,是警告,引发原因是你的操作虽然没有错,但却不合规范。第二次运行,仍然是不规范,但由于之前报过一次,这里就不再报了。

看内容,规范的操作应该是:

BeautifulSoup(content, "html5lib")
【热门文章】
【热门文章】