python 之beautiful soup 4 warning

在使用beautifulsoup4时出现此警告,必应后找到如下解决方案:

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.

UserWarning:没有明确指定解析器,因此我正在使用该系统的最佳可用HTML解析器(“html.parser”)。
这通常不是问题,但是如果您在另一个系统或不同的虚拟环境中运行此代码,
它可能会使用不同的解析器并且行为不同。

解决办法:指定HTML解析器

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/warandpeace.html")
bsObj = BeautifulSoup(html,'html.parser')
namelist=bsObj.findAll('span',{'class':'green'})
for name in namelist:
    print(name.get_text())

bsobj=BeautifulSoup(html,'html.parser')

完成。

猜你喜欢

转载自www.cnblogs.com/frank1126lin/p/9102523.html