select find和find_all有什么区别呢,请看代码:
from bs4 import BeautifulSoup html =‘‘‘<head>hello python</head> <body> <div class=‘ming‘> <div class=‘lily‘ id=‘ben‘>赵丽丽是个大笨蛋</div> <div class=‘lily‘ id=‘ben‘>赵丽丽是个大笨蛋吗?</div> <a href = ‘www.mingmig.com‘>我的网址</a> <a href = ‘www.mingmig.com‘>我的网址</a> </div> </body> ‘‘‘ soup = BeautifulSoup(html, ‘lxml‘) s = soup.select(‘div .lily‘)#select的写法和find有区别,select是标签和class都在一个字符串里,find是两个字符串,用逗号隔开 f = soup.find(‘div‘,class_ = ‘lily‘) #find只取第一个值,返回的是字符串 fa = soup.find_all(‘div‘,class_ = ‘lily‘)#find——all是全部的值和select一样,是一个列表 fal = soup.find_all(‘div‘,class_ = ‘lily‘,limit=1)#find——all是全部的值和select一样,是一个列表,加limit属性后只返回第一个 print(s) print(f) print(fa) print(fal)
>>>
[<div class="lily" id="ben">赵丽丽是个大笨蛋</div>, <div class="lily" id="ben">赵丽丽是个大笨蛋吗?</div>] <div class="lily" id="ben">赵丽丽是个大笨蛋</div> [<div class="lily" id="ben">赵丽丽是个大笨蛋</div>, <div class="lily" id="ben">赵丽丽是个大笨蛋吗?</div>] [<div class="lily" id="ben">赵丽丽是个大笨蛋</div>]