python使用selenium自動化加載Firefox配置文件執行post get方法

單純使用BeautifulSoup進行爬取百度貼吧首頁的時候,只能爬取到1-20條熱門動態裏面的圖片。爲了爬取到完整的熱門動態裏面的圖片,我們則需要模擬瀏覽器的滾動條滾動,讓網頁去觸發xhr請求更多的熱門動態。

安裝python插件

pip install selenium

安裝瀏覽器驅動

火狐瀏覽器驅動

谷歌瀏覽器驅動

opera瀏覽器驅動

將下載的文件解壓後添加到環境變量中。
添加到環境變量中

模擬Firefox瀏覽器行爲

  • 必須安裝瀏覽器和瀏覽器驅動,並且瀏覽器和瀏覽器驅動要匹配
  • 瀏覽器驅動所在的目錄要在環境變量中,或者定義瀏覽器browser的時候指定驅動的路徑
from selenium import webdriver
from bs4 import BeautifulSoup
import time
driver = webdriver.Firefox()
driver.get("https://tieba.baidu.com/index.html")
# 模擬滾動條滾動到底部
for i in range(1, 5):
    driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
    time.sleep(1)
html = BeautifulSoup(driver.page_source, "lxml")
imgs = html.select("#new_list li img")
driver.close()
driver.quit()

在這裏插入圖片描述

模擬Chrome瀏覽器行爲

在這裏插入圖片描述

from selenium import webdriver
driver = webdriver.Chrome() #使用默認自動化測試
driver.get(r'https://blog.csdn.net/zycdn')
driver.close()
drvier.quit()

加載chrome配置文件失敗

from selenium import webdriver
# 個人資料路徑
user_data_dir = r'--user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data'
# 加載配置數據
option = webdriver.ChromeOptions()
option.add_argument(user_data_dir)
# 啓動瀏覽器配置 
driver = webdriver.Chrome(chrome_options=option, executable_path=r'D:\bin\chromedriver.exe')
# 一直提示錯誤,所以用Firefox了
'''
Warning (from warnings module):
  File "__main__", line 1
DeprecationWarning: use options instead of chrome_options
Traceback (most recent call last):
selenium.common.exceptions.WebDriverException: Message: unknown error: failed to write prefs file
'''

加載chrome配置文件失敗

加載Firefox配置文件

幫助-故障排除信息
查看配置文件夾
有些插件需要手動開啓
有的擴展需要停用再啓用才能使用

from selenium import webdriver
# 配置文件路徑
profile_path = r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\cievpga3.default-release'
# 加載配置數據
profile = webdriver.FirefoxProfile(profile_path)
# 啓動瀏覽器配置
driver = webdriver.Firefox(firefox_profile=profile, executable_path=r'D:\bin\geckodriver.exe')
driver.get(r'http://****************.21tb.com')
driver.close()
driver.quit()

通過函數調用POST GET方法

在函數中通過post get方法獲取數據
在這裏插入圖片描述

from selenium import webdriver
# 配置文件路徑 幫助-故障排除信息
profile_path = r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\cievpga3.default-release'
# 加載配置數據
profile = webdriver.FirefoxProfile(profile_path)
# 啓動瀏覽器配置
driver = webdriver.Firefox(firefox_profile=profile, executable_path=r'D:\bin\geckodriver.exe')
driver.get(r'http://************************.21tb.com')
print(driver.execute_script('return test()')) #簡單的直接在函數中返回就可以
# gettest getpost是通過插件注入到頁面的
driver.execute_script('return gettest()') #複雜的就先執行函數,然後返回數據
print(driver.execute_script('return get_data'))
driver.execute_script('return posttest()')#複雜的就先執行函數,然後返回數據
print(driver.execute_script('return post_data'))
driver.close()
driver.quit()

#其他地方是這樣介紹執行JS函數的
#即使改成先定義再執行的方式也是不好使的
js=r"""
var url = "*****************";
axios.get(url).then(function (response) {
  total = response.data.data.total;
  return total;
});
"""
print(driver.execute_script(js))

注入工具

FeHelperWeb開發者助手
需要注入的代碼自己編寫就可以
注入的代碼

selenium更多用法

查找元素

from selenium import webdriver
from bs4 import BeautifulSoup
browser = webdriver.Firefox()
browser.get("https://tieba.baidu.com/index.html")
new_list = browser.find_element_by_id('new_list')
user_name = browser.find_element_by_name ('user_name')
active = browser.find_element_by_class_name  ('active')
p = browser.find_element_by_tag_name ('p')

# find_element_by_name 通過name查找單個元素
# find_element_by_xpath 通過xpath查找單個元素
# find_element_by_link_text 通過鏈接查找單個元素
# find_element_by_partial_link_text 通過部分鏈接查找單個元素
# find_element_by_tag_name 通過標籤名稱查找單個元素
# find_element_by_class_name 通過類名查找單個元素
# find_element_by_css_selector 通過css選擇武器查找單個元素
# find_elements_by_name 通過name查找多個元素
# find_elements_by_xpath 通過xpath查找多個元素
# find_elements_by_link_text 通過鏈接查找多個元素
# find_elements_by_partial_link_text 通過部分鏈接查找多個元素
# find_elements_by_tag_name 通過標籤名稱查找多個元素
# find_elements_by_class_name 通過類名查找多個元素
# find_elements_by_css_selector 通過css選擇武器查找多個元素

獲取元素信息

btn_more = browser.find_element_by_id('btn_more')
print(btn_more.get_attribute('class')) # 獲取屬性
print(btn_more.get_attribute('href')) # 獲取屬性
print(btn_more.text) # 獲取文本值

元素交互操作

btn_more = browser.find_element_by_id('btn_more')
btn_more.click() # 模擬點擊,可以模擬點擊加載更多

input_search = browser.find_element(By.ID,'q')
input_search.clear() # 清空輸入

執行JavaScript

browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')
browser.execute_script('alert("To Bottom")')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章