python爬蟲 Selenium庫學習

來源:互聯網
上載者:User

標籤:name   兩種方法   別人   nal   key   tag   sele   port   mod   

一、自動化測試載入器,支援多種瀏覽器,解決JS渲染問題二、安裝

pip3 install Selenium

三、操作介紹(因為是學習別人的課程為了尊重智慧財產權,部分代碼就不顯示了)1驅動瀏覽器

browser = webdriver.Chrome()

try:   
 browser.get(‘www.sina.com‘)#上網

2尋找元素 

一種方法:

browser.find_element_by_name()
browser.find_element_by_class_name()
browser.find_element_by_id()
browser.find_element_by_xpath()
browser.find_element_by_tag_name()

等等

第二種方法:

browser.find_element(By.NAME,‘search‘)

這裡面的find_element相當於BS4裡面的find,如果想要用find_all,就要用find_elements

同上有兩種方法,我這裡就寫一種了

browser.find_elements(By.NAME,‘search‘)

3互動操作

input = browser.find_element_by_class_name(‘search-combobox-input‘)
input.send_keys(‘機械鍵盤‘)
time.sleep(3)
input.clear()
input.send_keys(‘iPad‘)
time.sleep(3)
button = browser.find_element_by_class_name(‘btn-search‘)
button.click()

更多操作詳見官方文檔:

http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement

執行動作鏈:

感覺這用的不多,詳見官方文檔

http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

4擷取屬性 顯示內容

input = browser.find_element_by_class_name(‘search-combobox-input‘)

print(input.get_attribute(‘aria-combobox‘))

這樣返回的是aria-combobox 的屬性‘list’

顯示文本

browser = webdriver.Chrome()
try:
url = ‘https://www.zhihu.com/explore‘
browser.get(url)
answers = browser.find_elements_by_class_name(‘zh-summary‘)
for answer in answers:
print(answer.text)
finally:
browser.close()

這樣就顯示了知乎發現裡面答案的文本

擷取ID、位置、標籤名、大小

browser = webdriver.Chrome()
try:
url = ‘https://www.zhihu.com/explore‘
browser.get(url)
answers = browser.find_elements_by_class_name(‘zh-summary‘)
for answer in answers:

print(answer.location)
print(answer.tag_name)
print(answer.size)

finally:
browser.close()

Frame

在某些用到架構的網頁裡面使用:

browser.switch_to.frame(‘name‘)
browser.switch_to.parent_frame(‘name‘)

5等待

有的時候網路需要等待Ajax請求載入,所以需要等待網頁全部載入出來

隱式等待

當載入網頁沒有顯示我們需要找的元素的時候,會隱式地等待一段時,如果找到元素就直接返回

browser = webdriver.Chrome()

browser.implicitly_wait(10)

url = ‘https://www.zhihu.com/explore‘
browser.get(url)

顯式等待

顯式等待是如果特定條件不滿足則等待規定的時間

browser = webdriver.Chrome()
url = ‘https://www.zhihu.com/explore‘
browser.get(url)
wait = WebDriverWait(browser,10)
intput = wait.until(EC.presence_of_element_located((By.ID,‘q‘)))
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,‘***‘)))

顯式等待的條件見官方文檔:http://selenium-python.readthedocs.io/api.html#selenium.webdriver.support.expected_conditions.element_located_selection_state_to_be

title_is標題是某內容
title_contains標題包含某內容
presence_of_all_elements_located y元素載入出,傳入定位元組,如(By.ID,‘p‘)
visibility_of 元素可見,傳入定位元組

text_to_be_present_in_element 某個元素文本包含某個文字
text_to_be_present_in_element_value 某個元素值包含某檔案
element_to_be_clickable 某元素可點擊

等等

6瀏覽器前進和後退

browser.forward()
browser.back()

7Cookies

可以進行cookies的查看,添加和刪除操作

url = ‘https://www.zhihu.com/explore‘
browser.get(url)
print(browser.get_cookies())
browser.add_cookie({‘name‘:‘dai‘,‘value‘:‘123‘})#一定要記得有value否則會報錯
print(browser.get_cookies())
browser.delete_all_cookies()
print(browser.get_cookies())

python爬蟲 Selenium庫學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.