爬蟲之標籤查詢補充及selenium模組的安裝及使用與案例
阿新 • • 發佈:2020-09-18
今日內容概要
- bs模組之標籤查詢
- 過濾器
- selenium模組
今日內容詳細
html_doc = """ <html> <head> <title>The Dormouse's story</title> </head> <body> <p id="my p" class="title"> <b id="bbb" class="boldest">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> """ from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc,'lxml') #2、獲取標籤的名稱 # print(soup.p.name) #3、獲取標籤的屬性 # print(soup.p.attrs) #4、獲取標籤的內容 # print(soup.p.text) #5、巢狀選擇 # print(soup.head.title.string) # 依次往內部查詢 # print(soup.body.a.string) #6、子節點、子孫節點 # print(soup.p.contents) #p下所有子節點 # print(soup.p.children) #得到一個迭代器,包含p下所有的子節點 # for child in soup.p.children: # print(child) #7、父節點、祖先節點 # print(soup.a.parent) #獲取a標籤的父節點 # print(soup.a.parents) #找到a標籤所有的祖先節點,父親的父親,父親的父親的父親…… # for p in soup.a.parents # print(p) #8、兄弟節點 # print('=====>') # print(soup.a.next_siblings) #下一個兄弟 # for i in soup.a.next_siblings: # print(i) # print(soup.a.previons_sibling) #上一個兄弟 # print(list(soup.a.next_siblings)) #下面的兄弟們=>生成器物件 # print(soup.a.previous_siblings) #上面的兄弟們=>生成器物件 """ 兒子:在一個標籤內部的第一層級標籤 <span>相對於下面同級別的div就是哥哥</span> <div> <div>相對於外層的div就是兒子 <p>上一層div的兒子上上一層div的孫子</p> </div> <p>相對於外層的div就是兒子</p> </div> <span>相對上面同級別的div就是弟弟</span> 後代 兄弟 弟弟 哥哥 """
過濾器
from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc,'lxml') #1、五種過濾器:字串、正則表示式、列表、True、方法 #1.1、字串:即標籤名 結果是一個列表 裡面的元素才是真正的標籤物件 # print(soup.find_all('b')) # [<b> class="boldest" id="bbb">The Dormouse's story</b'] #1.2、正則表示式 # import re # 一定要注意拿到的結果到底是什麼資料型別 # print(soup.find_all(re.compile('^b'))) # 找出b開頭的標籤,結果有body和b標籤 #1.3、列表:如果傳入列表引數,Beautiful Soup會將與列表中任一元素匹配的內容返回。下面程式碼找到文件中所有<a>標籤和<b>標籤: # print(soup.find_all(['a','b'])) # 找到文件中所有<a>標籤和<b>標籤 #1.4、True:可以匹配任何值,下面程式碼查詢到所有的tag,但是不會返回字串節點 # print(soup.find_all(True)) # True表示所有 # for tag in soup.find_all(True): # print(tag.name) #1.5、方法:如狗哦沒有合適的過濾器,那麼還可以定義一個方法,方法只接受一個元素引數,如果這個方法返回 True 表示當前元素匹配並且被找到,如果不是返回 False # def has_class_but_no_id(tag): # return tag.has_attr('class') and not tag.has_attr('id') # # print(soup.find_all(has_class_but_no_id))
中文文件
https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-parents-find-parent
selenium模組
嫩夠幫你自動操作瀏覽器 selenium最初是一個自動化測試工具,而爬蟲中使用它主要是為了解決requests無法直接執行JavaScript程式碼的問題 selenium本質是通過驅動瀏覽器,完全模擬瀏覽器的操作,比如跳轉、輸入、點選、下拉等,來拿到網頁渲染之後的結果,可支援多種瀏覽器 1.需要下載模組 pip3 install -i 2.還需要下載一個控制瀏覽器的驅動 http://npm.taobao.org/mirrors/chromedriver/2.38/ # 注意下載的驅動一定呀跟你瀏覽器的版本匹配 不能無法操作 如何檢視當前瀏覽器版本 3.下載好的驅動檔案有兩個存放的位置 1.直接放在你專案的目錄下 2.放到Python安裝路徑的scripts目錄中即可 from selenium import webdriver import time bro=webdriver.Chrome() # 生成谷歌瀏覽器的驅動物件 bro.get("http://www.baidu.com") # 自動開啟谷歌瀏覽器訪問百度首頁 bro.implicitly_wait(10) # 設定一個等待時間 超出範圍還沒加載出來就放棄 # 1、find_element_by_id 根據id找 # 2、find_element_by_link_text 根據連結名字找到控制元件(a標籤的文字) # 3、find_element_by_partial_link_text 根據連結名字找到控制元件(a標籤的文字)模糊查詢 # 4、find_element_by_tag_name 根據標籤名 # 5、find_element_by_class_name 根據類名 # 6、find_element_by_name 根據屬性名 # 7、find_element_by_css_selector 根據css選擇器 aEle = bro.find_element_by_link_text('登入') # 點選a標籤 aEle.click() # 通過id獲取p標籤 pEle = bro.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn') # 點選p標籤 pEle.click() # 通過id找獲取使用者使用者名稱的input框 UserEle = bro.find_element_by_id('TANGRAM__PSP_11__userName') # 點選UserEle UserEle.click() # 輸入使用者名稱 UserEle.send_keys('18856211855') time.sleep(1) # 通過id找獲取使用者密碼的input框 PwdEle = bro.find_element_by_id('TANGRAM__PSP_11__password') # 點選UserEle PwdEle.click() # 輸入使用者名稱 PwdEle.send_keys('xxxxxx') time.sleep(1) SubEle = bro.find_element_by_id('TANGRAM__PSP_11__submit') SubEle.click() print(bro.get_cookies()) # 直接組織成字典裡面有所有的鍵值對 print(bro.get_cookie()) # Get a single cookie by name bro.close()
等待元素載入完畢
"""
有些頁面的標籤元素並不是直接寫在html檔案上的而是通過後續
js程式碼動態加載出來的 所以需要在查詢標籤的時候設定一個等待時間
"""
# 隱式等待:在查詢所有元素時,如果尚未被載入,則等10秒(推薦使用)
# browser.implicitly_wait(10) 表示等待所有,
# 顯式等待:顯式地等待某個元素被載入(不推薦使用)
# wait=WebDriverWait(browser,10)
# wait.until(EC.presence_of_element_located((By.ID,'content_left')))