Python爬虫,通过特定的函数来筛选标签

这个倒是很新奇,我一直都没想到可以这么玩。
不过确实有趣~ 这是在看书的时候,无意之中看到的源码。

通过Soup元素的findAll函数
注意,这里,finfAll是函数(function),不是方法(method)

from urllib.request import urlopen
from bs4 import BeautifulSoup

if __name__ == '__main__':
    url = 'https://movie.douban.com/subject/1302827/'
    htmlObj = urlopen(url)
    soup = BeautifulSoup(htmlObj.read(), 'lxml')
    tags = soup.findAll(lambda text: len(text.attrs) == 2)
    print(tags[5])
    原文作者:肥宅_Sean
    原文地址: https://blog.csdn.net/a19990412/article/details/79595170
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞