代碼:
# -*- encoding: utf-8 -*-
"""
@project = Pa_chong
@file = test2
@auther = ztt
@create_time = '2019/4/13 9:17'
"""
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/warandpeace.html")
bsObj = BeautifulSoup(html)
nameList = bsObj.find_all("span", {"class": "green"})
for name in nameList:
print(name.get_text())
運行後報警告:
原因:
- 需要
html5lib
庫的支持
解決:
- 安裝:
pip install html5lib
bsObj = BeautifulSoup(html)
更改爲bsObj = BeautifulSoup(html, "html5lib")