如果我们用BeautifulSoup去解析网页来爬虫,那么90%以上需要用到find_all(),当然如果对CSS更为了解,其实select也是一种相当不错的选择。
from bs4 import BeautifulSoup
soup = BeautifulSoup(response.text, 'lxml')
这是我们常用的炖汤程序,而对于汤的赏析,就需要用soup.find_all(),soup.select()去细细品味其中滋味。
1、find_all()
find_all(tag,attributes,recurisive, text, limit, keywords)
tag,即标签名,可以寻找单个标签find_all('h1'),也可以寻找一堆标签find_all(['h1','h2','h3'])
attributes,属性,即通过标签具有的属性来查找标签,其属性参数需要用字典封装。用法如 find_all(attr={'class':'red'}),或者find_all('class_' = 'red')。