python爬蟲(1)——BeautifulSoup庫函數find_all() (轉)

原文地址:http://blog.csdn.net/depers15/article/details/51934210

python——BeautifulSoup庫函數find_all()

1、語法介紹

find_all( name , attrs , recursive , string , **kwargs ) 
find_all() 方法搜索當前tag的全部tag子節點,並判斷是否符合過濾器的條件

2、參數及用法介紹

一、name參數

這是最簡單而直接的一種辦法了,我麼能夠經過html標籤名來索引;

sb = soup.find_all('img')

二、keyword參數

所謂關鍵字參數其實就是經過一個html標籤的id、href(這個主要指的是a標籤的 )和title,我測試了class,這個方法好像不行,不過沒有關係,下面我會談到這個點的!
soup.find_all(href=re.compile("elsie"))
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]

soup.find_all(id='link2')
# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

這裏的true指的就是選中全部有id這個屬性的標籤;
soup.find_all(id=True)
# [<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(href=re.compile("elsie"), id='link1')
# [<a class="sister" href="http://example.com/elsie" id="link1">three</a>]

還有有些屬性在搜索時就不能使用,就好比HTML5中的 data-* 屬性,咋辦?

data_soup = BeautifulSoup('<div data-foo="value">foo!</div>')
data_soup.find_all(data-foo="value")
# SyntaxError: keyword can't be an expression

可是能夠經過 find_all() 方法的 attrs 參數定義一個字典參數來搜索包含特殊屬性的tag:

data_soup.find_all(attrs={"data-foo": "value"})
# [<div data-foo="value">foo!</div>]

雖然咱們不能像id他們那樣使用,由於class在python中是保留字(保留字(reserved word),指在高級語言中已經定義過的字,使用者不能再將這些字做爲變量名或過程名使用。 
),因此呢,直接使用是回報錯的,因此class_應運而生; 
因此呢,順便上一張圖片,讓咱們看一看python都有哪些保留字: 
![](http://images2017.cnblogs.com/blog/825729/201709/825729-20170929143447247-961841526.png)
經過標籤名和屬性名一塊兒用:
soup.find_all("a", class_="sister")
# [<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>]
除此以外呢,還有就是class_ 參數一樣接受不一樣類型的 過濾器 ,字符串,正則表達式,方法或 True :固然,上面的屬性也能夠和標籤名結合起來使用;
soup.find_all(class_=re.compile("itl"))
# [<p class="title"><b>The Dormouse's story</b></p>]

def has_six_characters(css_class):
    return css_class is not None and len(css_class) == 6

soup.find_all(class_=has_six_characters) 
#這裏的這個函數,其實就是一個布爾值True;
# [<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>]

3.sting參數

經過 string 參數能夠搜搜文檔中的字符串內容.與 name 參數的可選值同樣, string 參數接受 字符串 , 正則表達式 , 列表, True;css

soup.find_all("a", string="Elsie")
# [<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>]

4.limit參數

這個參數其實就是控制咱們獲取數據的數量,效果和SQL語句中的limit同樣;html

soup.find_all("a", limit=2)
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

5.recursive參數

調用tag的 find_all() 方法時,Beautiful Soup會檢索當前tag的全部子孫節點,若是隻想搜索tag的直接子節點,可使用參數 recursive=False;
Html:python

<html>
 <head>
  <title>
   The Dormouse's story
  </title>
 </head>
...

python:正則表達式

soup.html.find_all("title")
# [<title>The Dormouse's story</title>]

soup.html.find_all("title", recursive=False)
# []

因此他只獲取本身的直接子節點,也就是他本身,這個標籤本身就是他的直接子節點;express

Beautiful Soup 提供了多種DOM樹搜索方法. 這些方法都使用了相似的參數定義. 好比這些方法: find_all(): name, attrs, text, limit. 可是隻有 find_all() 和 find() 支持 recursive 參數.
相關文章
相關標籤/搜索