爬虫入门-3.初识BeautifulSoup
生活随笔
收集整理的這篇文章主要介紹了
爬虫入门-3.初识BeautifulSoup
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一.安裝BeautifulSoup
BeautifulSoup是一個可以從HTML或XML文件中提取數(shù)據(jù)的Python庫.它能夠通過你喜歡的轉(zhuǎn)換器實現(xiàn)慣用的文檔導(dǎo)航,查找,修改文檔的方式.BeautifulSoup配合Request使用,能大大提高爬蟲效率。
pip install BeautifulSoup
二.常見操作
from bs4 import BeautifulSouphtml_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ # # 將一段文檔傳入BeautifulSoup 的構(gòu)造方法,就能得到一個文檔的對象, 可以傳入一段字符串或一個文件句柄 soup1 = BeautifulSoup("<html>data</html>", 'lxml') print(soup1) soup = BeautifulSoup(html_doc, "lxml") print(soup.title) print(soup.head) # 通過點取屬性的方式只能獲得當(dāng)前名字的第一個tag: print(soup.a) # 如果想要得到所有的<a>標(biāo)簽,或是通過名字得到比一個tag更多的內(nèi)容的時候,就需要用到 Searching the tree 中描述的方法,比如: find_all() print(soup.find_all('a')) # return List print(soup.find(id='link2')) # 從文檔中找到所有<a>標(biāo)簽的鏈接 for link in soup.find_all('a'):print(link.get('href')) # # 從文檔中獲取所有文字內(nèi)容 print(soup.get_text()) ''' Beautiful Soup將復(fù)雜HTML文檔轉(zhuǎn)換成一個復(fù)雜的樹形結(jié)構(gòu),每個節(jié)點都是Python對象,所有對象可以歸納為4種:Tag , NavigableString , BeautifulSoup , Comment . ''' soup = BeautifulSoup('<b class="boldest">Extremely bold</b>', 'lxml') tag = soup.b print(tag.attrs) # print(tag['class']) 兩者一樣 print(tag.string) # 文本 # tag中包含的字符串不能編輯,但是可以被替換成其它的字符串,用 replace_with() 方法: tag.string.replace_with("No longer bold") print(tag.string) # '''遍歷文檔樹''' soup = BeautifulSoup(html_doc, "lxml") # # .string # # 如果tag只有一個 NavigableString 類型子節(jié)點,那么這個tag可以使用 .string 得到子節(jié)點: head_tag = soup.head print(head_tag.contents) title_tag = head_tag.contents[0] print(title_tag) print(title_tag.contents) print(title_tag.string) # 如果tag包含了多個子節(jié)點,tag就無法確定 .string 方法應(yīng)該調(diào)用哪個子節(jié)點的內(nèi)容, .string 的輸出結(jié)果是 None : # 那么可以使用 .strings 來循環(huán)獲取 # ----- find_all()參數(shù)注解------ # find_all( name , attrs , recursive , text , **kwargs ) # find_all() 方法搜索當(dāng)前tag的所有tag子節(jié)點,并判斷是否符合過濾器的條件 alist = soup.find_all("a", class_="sister") print(alist) # 1.通過 text 參數(shù)可以搜搜文檔中的字符串內(nèi)容.與 name 參數(shù)的可選值一樣, text 參數(shù)接受 字符串 , 正則表達式 , 列表, True . print(soup.find_all(text="Lacie")) # 2.limit 參數(shù) # find_all() 方法返回全部的搜索結(jié)構(gòu),如果文檔樹很大那么搜索會很慢.如果我們不需要全部結(jié)果,可以使用 limit 參數(shù)限制返回結(jié)果的數(shù)量. # 當(dāng)搜索到的結(jié)果數(shù)量達到 limit 的限制時,就停止搜索返回結(jié)果. print(soup.find_all("a", limit=2)) # 3.recursive 參數(shù) # 調(diào)用tag的 find_all() 方法時,Beautiful Soup會檢索當(dāng)前tag的所有子孫節(jié)點,如果只想搜索tag的直接子節(jié)點, # 可以使用參數(shù) recursive=False . # 4.name 參數(shù)? # name 參數(shù)可以查找所有名字為 name 的tag,字符串對象會被自動忽略掉. soup.find_all("a") # 5.keyword 參數(shù) # 如果一個指定名字的參數(shù)不是搜索內(nèi)置的參數(shù)名,搜索時會把該參數(shù)當(dāng)作指定名字tag的屬性來搜索,如果包含一個名字為 id 的參數(shù), # Beautiful Soup會搜索每個tag的”id”屬性. soup.find_all(id='link2')
轉(zhuǎn)載于:https://www.cnblogs.com/min-R/p/10506612.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的爬虫入门-3.初识BeautifulSoup的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cookie html5,HTML5——
- 下一篇: c语言中竖线的作用,竖线符号