Python爬蟲利器Beautiful Soup入門詳解,實戰總結?。?!

      網友投稿 1248 2022-05-29

      1、簡介

      2、解析庫

      3、講解

      3.1、Tag(標簽選擇器)

      3.2、標準選擇器(find、find_all)

      3.2.1、find_all()

      3.2.2、find()

      1、簡介

      2、解析庫

      3、講解

      3.1、Tag(標簽選擇器)

      3.2、標準選擇器(find、find_all)

      3.2.1、find_all()

      3.2.2、find()

      3.3、Select選擇器

      4、實戰

      1、簡介

      Beautiful Soup 是一個可以從HTML或XML文件中提取數據的Python庫.它能夠通過你喜歡的轉換器實現慣用的文檔導航,查找,修改文檔的方式.Beautiful Soup會幫你節省數小時甚至數天的工作時間.

      2、解析庫

      靈活又方便的網頁解析庫,處理高效,支持多種解析器。

      利用它不用編寫正則表達式即可方便地實現網頁信息的提取。

      3、講解

      3.1、Tag(標簽選擇器)

      ==選擇元素==

      import requests from bs4 import BeautifulSoup html = ''' The Dormouse's story

      The Dormouse's story

      Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

      ...

      ''' #使用BeautifulSoup對網頁代碼進行解析 #我這里使用的是Python標準庫——html.parser soup = BeautifulSoup(html, "html.parser") # 獲取html代碼中的titile標簽 print(soup.title)

      注意:這里默認只匹配第一個,如果文章中有多個相同的標簽,而且想要獲取之后的標簽,可根據class值或者一些其他方法進行定位,之后我會一一道來。

      ==獲取名稱==

      print(soup.title.name)

      ==獲取屬性==

      ==獲取內容==

      ==嵌套選擇==

      ==子節點==

      tag的 .contents 屬性可以將tag的子節點以列表的方式輸出

      通過tag的 .children 生成器,可以對tag的子節點進行循環

      import requests from bs4 import BeautifulSoup html = ''' The Dormouse's story

      The Dormouse's story

      Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

      ...

      ''' soup = BeautifulSoup(html, "html.parser") print(soup.p.contents) print("="*30) for i in soup.p.children: print(i)

      ==父節點==

      通過 .parent 屬性來獲取某個元素的父節點

      通過元素的 .parents 屬性可以遞歸得到元素的所有父輩節點

      ==兄弟節點==

      3.2、標準選擇器(find、find_all)

      find_all( name , attrs , recursive , string , **kwargs )

      find_all() 方法搜索當前tag的所有tag子節點,并判斷是否符合過濾器的條件

      ==keyword 參數==

      Python爬蟲利器之Beautiful Soup入門詳解,實戰總結?。?!

      如果一個指定名字的參數不是搜索內置的參數名,搜索時會把該參數當作指定名字tag的屬性來搜索,如果包含一個名字為 id 的參數,Beautiful Soup會搜索每個tag的”id”屬性.

      ==自定義參數查找:attrs==

      find( name , attrs , recursive , text , **kwargs )

      find返回單個元素,find_all返回所有元素

      3.3、Select選擇器

      ==select==

      匹配全部

      import requests from bs4 import BeautifulSoup html = ''' The Dormouse's story

      The Dormouse's story

      Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

      ...

      ''' soup = BeautifulSoup(html, "html.parser") print(soup.select("p b")) print(soup.select("p a")) print(soup.select("head title"))

      ==select_one==

      select_one只選擇滿足條件的第一個元素

      4、實戰

      本次實戰以百度首頁為例

      import requests from bs4 import BeautifulSoup headers = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36" } url = "https://www.baidu.com" response = requests.get(url=url,headers=headers) soup = BeautifulSoup(response.text,"html.parser") #獲取全部class為mnav c-font-normal c-color-t的標簽,進行遍歷 divs = soup.find_all(class_="mnav c-font-normal c-color-t") for div in divs: print(div) print("="*40)

      可見獲取成功

      接下來獲取每個模塊對應的URL和文本值

      for div in divs: print(div['href']) print(div.text)

      import requests from bs4 import BeautifulSoup headers = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36" } url = "https://www.baidu.com" response = requests.get(url=url,headers=headers) soup = BeautifulSoup(response.text,"html.parser") #第一種方法 #通過contents,獲取子節點信息 a_data = soup.find(class_="hot-title").contents print(a_data[0].text) #第二種方法 #先通過find使用class值定位,在使用find找到其下的div標簽也就是我們需要的 a_data2 = soup.find(class_="hot-title").find("div") print(a_data2.text)

      博主會持續更新,有興趣的小伙伴可以、關注和下哦,你們的支持就是我創作最大的動力!

      HTML

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:dedecms內容頁調用圖片集文檔的圖集圖片
      下一篇:ABAP Netweaver服務器的標準登錄方式講解
      相關文章
      国产精品日本亚洲777| 亚洲成AⅤ人影院在线观看| 亚洲精品乱码久久久久久蜜桃图片| 亚洲视频在线观看网站| 亚洲日韩av无码| 国产亚洲精品国看不卡| 亚洲性久久久影院| 国产精品久久久久久亚洲小说| 亚洲av无码无线在线观看| 亚洲AV日韩AV无码污污网站| 久久人午夜亚洲精品无码区| 亚洲AV综合色区无码一二三区 | 国产偷v国产偷v亚洲高清| 最新亚洲成av人免费看| 亚洲熟妇丰满多毛XXXX| 亚洲色爱图小说专区| 亚洲va无码va在线va天堂| 久久精品国产亚洲AV麻豆不卡| 久久久久亚洲AV成人无码| 亚洲色四在线视频观看| 亚洲色大成网站www永久| 亚洲男女一区二区三区| 亚洲国产成人91精品| 亚洲看片无码在线视频| 亚洲人成欧美中文字幕| 色噜噜噜噜亚洲第一| 亚洲国产精品成人久久蜜臀 | 亚洲 国产 图片| 亚洲AV伊人久久青青草原| 亚洲片国产一区一级在线观看 | 亚洲s色大片在线观看| 亚洲爱情岛论坛永久| 久久久久久a亚洲欧洲AV| 久久亚洲精品专区蓝色区| 香蕉大伊亚洲人在线观看| 波多野结衣亚洲一级| 亚洲制服丝袜第一页| 亚洲小说图区综合在线| 国产成人亚洲精品播放器下载| 亚洲精品国产自在久久| 亚洲性猛交XXXX|