from bs4 import BeautifulSoup
with open('D:/BaiduNetdiskDownload/爬虫VIP课程六期课件/day05-08-05-数据解析之bs4/素材/三国演义.html', 'r', encoding='UTF-8') as f:
data = f.read()
#print(data)
soup = BeautifulSoup(data, 'lxml')
# print(soup)
a_list = soup.select('.book-mulu>ul>li>a')
print(a_list)
for a in a_list:
href = a['href']
title = a.text
print(title)
结果打印的a_list的返回值为空列表。[]
是什么原因呢?从头检查,data读取没有问题,读取的是html源代码。如下图所示
接下来可能就是
a_list = soup.select('.book-mulu>ul>li>a')
但是从逻辑上看也没有问题,从class=“book-mulu”到下一个ul标签、li、a标签。
问题原来在这里,括号前后要留有空格!!!