find 和 find_all 用法


soup = BeautifulSoup(requests.get(url).text, 'html.parser')

soup.find('span', class_='item_hot_topic_title')     這個是只能找到第一個span標簽 樣式為 class='item_hot_topic_title',就算后面還有匹配的也不去獲取

span.find_all('span', class_='item_hot_topic_title')  這個就能找到頁面上所有span標簽 樣式為 class='item_hot_topic_title'

 

soup.find_all(["a", "b"])   找到所有a和b的標簽已數組形式返回,傳參為 True 返回頁面所有標簽已數組形式

其他用法:

soup.find_all("title") # [<title>The Dormouse's story</title>] soup.find_all("p", "title") # [<p class="title"><b>The Dormouse's story</b></p>] soup.find_all("a") # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, # <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] soup.find_all(id="link2") # [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>] import re soup.find(text=re.compile("sisters")) # u'Once upon a time there were three little sisters; and their names were\n'

其他方法見官網真的很多https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id85


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM