python3 BeautifulSoup(YOUR_MARKUP, "html.parser")错误

发布时间:2020-09-10 21:34     分类:Python
是用python的BeautifulSoup模块是报错了,错误代码如下:
UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). 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.
The code that caused this warning is on line 16 of the file D:/pycharm36/demo/demo1.py. To get rid of this warning, change code that looks like this:
 BeautifulSoup(YOUR_MARKUP})
to this:
 BeautifulSoup(YOUR_MARKUP, "html.parser")
  markup_type=markup_type))

代码是这样的:
url = 'https://www.baidu.com/'
r = requests.get(url,headers=header)
html = r.text
soup = BeautifulSoup(html)

header是自定义的header,没有什么问题
问题就在BeautifulSoup方法的参数,BeautifulSoup(html)还有第二个参数没有传,其实不传的也不影响代码往下运行,只是在控制台会报出红色的警告代码不太好看
第二个参数就是  ‘html.parser’  意思是告诉它前面一个参数的格式是什么样的

修改后完整代码如下:
url = 'https://www.baidu.com/'
r = requests.get(url,headers=header)
html = r.text
soup = BeautifulSoup(html,'html.parser')

这样就运行就不会把那样的错误了

评论

推荐文章