python調用chrome實現網頁自動操作過程
一. 內容簡介
python調用chrome實現網頁自動操作。
二. 軟件環(huán)境
2.1vsCode 2.2Anaconda
version: conda 22.9.0
2.3代碼
鏈接:
三.主要流程
3.1 下載驅動和插件
調用谷歌瀏覽器,需要下載瀏覽器驅動(https://registry.npmmirror.com/binary.html?path=chromedriver/),下載對應的版本,最新的里面沒有,網上找一下就可以了,谷歌或者csdn,
然后安裝 selenium
! pip install selenium ! pip install pyautogui
安裝成功
3.2 調用谷歌瀏覽器
這個驅動沒有路徑,好像也可以運行,我只指定了網址,瀏覽的exe路徑,就沒了
# 填寫鏈接 url_survey = 'https://www.wjx.cn/vm/ev6IfcA.aspx' options = webdriver.ChromeOptions() options.add_experimental_option('excludeSwitches', ['enable-automation']) options.add_experimental_option('useAutomationExtension', False) # 谷歌瀏覽器exe位置 options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe" # 是否要啟動頁面 # options.add_argument("--headless") # 啟用無頭模式 # GPU加速有時候會出bug options.add_argument("--disable-gpu") # 禁用GPU加速 options.add_argument("--disable-blink-features=AutomationControlled") driver = webdriver.Chrome(options=options) driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'}) # 啟動要填寫的地址,這就啟動瀏覽器 driver.get(url_survey) # 這是關閉瀏覽器 driver.quit()#停止
啟動成功
3.3 進行網頁操作
需要注意的就是,然后就是按自己的需求寫就好了,有啥其他用到的,以后再補充,現在就用到這一個
# 兩個功能一致,使用這個代碼找到對應的代碼,click()即可,和js基本類似,只是部分語法不一樣 # python里面的寫 sinPro = driver.find_elements(By.CSS_SELECTOR, f'.jqradio') # js里面的 document.querySelectorAll(".jqradio")
# 引入相關模塊 from selenium.webdriver.common.by import By from selenium import webdriver #隨機數產生 import random #延時 import time import pyautogui #單選題 def single(driver): # 假設有10個單選題 for j in range(1, 18): # 每個單選題所在的位置 sinPro = driver.find_elements(By.CSS_SELECTOR, f'#div{j}') # 每個單選題的答案進行遍歷 for answer in sinPro: # 對應每個單選題的選項組合 ansItem = answer.find_elements(By.CSS_SELECTOR, '.ui-radio') if ansItem: random.choice(ansItem).click() else: ansItem = answer.find_elements(By.CSS_SELECTOR, '.ui-checkbox') selected_items = random.sample(ansItem, random.randint(2, 4)) # 選擇兩個不重復的元素 for item in selected_items: item.click() # 答題時間間隔 time.sleep(random.randint(0, 1)/2) #腳本執(zhí)行方法 def launch(nums): for i in range(0, nums): url_survey = 'https://www.wjx.cn/vm/ev6IfcA.aspx' options = webdriver.ChromeOptions() options.add_experimental_option('excludeSwitches', ['enable-automation']) options.add_experimental_option('useAutomationExtension', False) options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe" # options.add_argument("--headless") # 啟用無頭模式 options.add_argument("--disable-gpu") # 禁用GPU加速 options.add_argument("--disable-blink-features=AutomationControlled") driver = webdriver.Chrome(options=options) driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'}) # 啟動要填寫的地址 driver.get(url_survey) # 填寫選擇題 # single(driver) # # 提交按鈕 # end = driver.find_elements(By.CSS_SELECTOR, f'#ctlNext') # end[0].click() #提交按鈕 time.sleep(4) print('已經提交了{}次問卷'.format(int(i) + int(1))) driver.quit()#停止 if __name__ == "__main__": #填寫問卷次數 launch(4000)
結果如圖
四.參考
【python】自動填寫問卷星問卷及提交 http://t.csdnimg.cn/aifYa
到此這篇關于python調用chrome實現網頁自動操作的文章就介紹到這了,更多相關python 網頁自動操作內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
django框架基于模板 生成 excel(xls) 文件操作示例
這篇文章主要介紹了django框架基于模板 生成 excel(xls) 文件操作,結合具體實例形式分析了Django框架基于模板生成excel的實現步驟與相關操作技巧,需要的朋友可以參考下2019-06-06Python3二分查找?guī)旌瘮礲isect(),bisect_left()和bisect_right()的區(qū)別
這篇文章主要介紹了Python3二分查找?guī)旌瘮礲isect(),bisect_left()和bisect_right()的區(qū)別,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03使用Python的Bottle框架寫一個簡單的服務接口的示例
這篇文章主要介紹了使用Python的Bottle框架寫一個簡單的服務接口的示例,基于Linux系統(tǒng)環(huán)境,需要的朋友可以參考下2015-08-08詳解利用Python scipy.signal.filtfilt() 實現信號濾波
這篇文章主要介紹了詳解利用Python scipy.signal.filtfilt() 實現信號濾波,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06