pythonselenium代理
① python selenium設置瀏覽器參數
設置瀏覽器參數
為了避免每次運行程序都打開一個窗口,廳慧我們也可以設置無窗口訪問,只需添加瀏覽器參數即可:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
url = ' https://www..com/'
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('lang=zh_CN.utf-8')
UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
chrome_options.add_argument('User-Agent=' + UserAgent)
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.maximize_window()
driver.minimize_window()
driver.get(url)
print(driver.title)
常用參數
啟動就最大化--start-maximized
指定用戶文件夾 User Data 路徑,可以把書簽這樣的用戶數扮敗答據保存在系統分區以外的分區–-user-data-dir=」[PATH]」
指定緩存Cache路徑–-disk-cache-dir=「[PATH]」 指定Cache大小,單位Byte–-disk-cache-size=100
隱身模式啟動–-incognito
禁用javascript–-disable-javascript# 禁止載入所有插件,可以增加速度--disable-plugins
禁用JavaScript--disable-javascript
禁用彈出攔截--disable-popup-blocking
禁用插件--disable-plugins
禁用圖像--disable-images
設置枯碼代理chrome_options.add_argument('--proxy-server=http://{ip}:{port}') #http://127.1.1.10:8080
② 如何在python中使用selenium
1、下載Python2.7版本,默認運行安裝即可;
2、安裝完成之後,設置Python環境變數C:\Python27(操作步驟: 電腦->屬性->高級->環境變數->系統變數中的PATH為:變數值: ;C:\Python27 )
3、在python的官方網站上可以找到SetupTools的下載,解壓安裝安裝即可;
4、當安裝SetupTools之後,就可以在python安裝目錄下看到Script目錄,
5、同樣在變數中加入 path:C:\Python27\Scripts,
6、打開cmd命令行,將目錄切換到C:\Python27\Scripts下,輸入命令「easy_install pip「安裝pip;
7、安裝成功pip之後,執行pip install -U selenium 進行下載安裝最新selenium的版本。
③ python selenium怎麼配置IE和chrome的代理,求代碼
請參考這個代碼
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def create_proxyauth_extension(proxy_host, proxy_port,
proxy_username, proxy_password,
scheme='http', plugin_path=None):
"""Proxy Auth Extension
args:
proxy_host (str): domain or ip address, ie proxy.domain.com
proxy_port (int): port
proxy_username (str): auth username
proxy_password (str): auth password
kwargs:
scheme (str): proxy scheme, default http
plugin_path (str): absolute path of the extension
return str -> plugin_path
"""
import string
import zipfile
if plugin_path is None:
plugin_path = '/tmp/vimm_chrome_proxyauth_plugin.zip'
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background"首段臘: {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""
background_js = string.Template(
"""
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "${scheme}",
host: "${host}",
port: parseInt(${port})
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "者滑${username}",
password: "${password}"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
""燃返"
).substitute(
host=proxy_host,
port=proxy_port,
username=proxy_username,
password=proxy_password,
scheme=scheme,
)
with zipfile.ZipFile(plugin_path, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
return plugin_path
proxyauth_plugin_path = create_proxyauth_extension(
proxy_host="proxy.crawlera.com",
proxy_port=8010,
proxy_username="",
proxy_password=""
)
co = Options()
co.add_argument("--start-maximized")
co.add_extension(proxyauth_plugin_path)
driver = webdriver.Chrome(chrome_options=co)
driver.get("wser.org/")
④ 用python,selenium,打開網頁或者點擊按鈕會向伺服器發送很多請求,用python如何獲
你可以在chrome上加一個代理,比如tiny proxy,它是純python寫的。你改一改就可以奕成抓包程序, 自然就可以獲得返回的請求。
另外在selenium里,也應該可以獲得響應結果。它通過chrome驅動,我沒有仔細看過代碼,webkit內核是直接通過toHtml,或者是類似的命令可以獲得響應結果的。 selenium應該也可以。
猜可以通過javascript驅動selenium,獲得響應結果。
⑤ 關於Python的Selenium框架全解,一篇完整的說明書
目錄
安裝 selenium 第三方庫
下載瀏覽器驅動:
需要把這些瀏覽器驅動放入 Python 應用目錄裡面的 Script 文件夾裡面
① 200 多本 Python 電子書(和經典的書籍)應該有
② Python標准庫資料(最全中文版)
③ 項目源碼(四五十個有趣且可靠的練手項目及源碼)
④ Python基礎入門、爬蟲、網路開發、大數據分析方面的視頻(適合小白學習)
⑤ Python學習路線圖(告別不入流的學習)
私信我01即可獲取大量Python學習資源
每次當selenium啟動chrome瀏覽器的時候,chrome瀏覽器很乾凈,沒有插件、沒有收藏、沒有 歷史 記錄,這是因為selenium在啟動chrome時為了保證最快的運行效率,啟動了一個裸瀏覽器,這就是為什麼需要配置參數的原因,但是有些時候我們需要的不僅是一個裸瀏覽器
selenium啟動配置參數接收是ChromeOptions類,創建方式如下 :
創建了ChromeOptions類之後就是添加參數,添加參數有幾個特定的方法,分別對應添加不同類型的配置項目
常用配置參數:
其他配置項目參數
製作無頭瀏覽器
規避檢測
門戶網站檢測如果是selenium請求的,有可能會拒絕訪問。這也是一種反爬機制
實現規避檢測
注意:這里只能使用 options 添加
如果有其他的模塊要添加,注意要分開添加
元素定位語法
常用語法:
在 element 變成 elements 時,返回符合條件的所有元素組成的數組
控制瀏覽器大小
瀏覽器後退,前進
刷新
在搜索框模擬回車操作
在 WebDriver 中, 將這些關於滑鼠操作的方法封裝在 ActionChains 類提供
ActionChains 類提供了滑鼠操作的常用方法:
語法:
想使用selenium中的鍵盤事件,首先我們必須導入Keys包,需要注意的是包名稱Keys首字母需要大寫。Keys類中提供了幾乎所有的鍵盤事件包括組合按鍵如 Ctrl+A、 Ctrl+C 等
使用語法:
其他事件可以通過查看源碼獲取
顯式等待使WebdDriver等待某個條件成立時繼續執行,否則在達到最大時長時拋出超時異常
實例:
WebDriverWait類是由WebDirver 提供的等待方法。在設置時間內,默認每隔一段時間檢測一次當前頁面元素是否存在,如果超過設置時間檢測不到則拋出異常
語法:
參數:
如果某些元素不是立即可用的,隱式等待是告訴WebDriver去等待一定的時間後去查找元素。 默認等待時間是0秒,一旦設置該值,隱式等待是設置該WebDriver的實例的生命周期
案例
語法:
alert 裡面的方法
WebDriver操作cookie的方法:
參考鏈接: https://www.jianshu.com/p/773c58406bdb
與普通的在headers里添加 {'Cookies':' '} 不一樣的是,此方法需要按照cookie的name,value,path,domain格式逐個cookie添加
通過execute_script()方法執行JavaScripts代碼來移動滾動條的位置
⑥ 如何實現Python與javascript通信
程序需要python和js混合編程,如何在python獲取js中變數的值。
是web的話,用http通信就好了
用python selenium吧,通過selenium代理python和瀏覽器之間的通信。
如果非要糾結是否遠程調用而不是本地數據調用,可以試試PyQt4,
裡面有一個QWebKit模塊(最新版本的PyQt5去掉Webkit了用的另外一個模塊),
PyQt5和PyQt4功能雷同,可以認為是2個分支
Webkit裡面有Python和JS的交互,甚至你可以用PyQt4做一個瀏覽器
⑦ Selenium 和python是啥關系是否相關參考文檔
Selenium是一個軟體測試工具; python是一種面向對象、直譯式計算機程序設計語言.
Selenium分成Selenium IDE, Selenium RC (Remote Control), 和Selenium Grid. 他們都是基於Selenium Core的. 這里給你說說Selenium IDE和RC吧. Selenium IDE是Firefox的一個插件, 它可以錄制一系列的過程(比如google中輸入一個單詞, 點搜索按鈕,...,). Selenium IDE可以把這個過程錄制號, 並且轉化成多種語言, 比如:Java, C#, Perl, PHP, Python,等等吧.
那麼這時候我們就可以這些語言對應的編譯器(比如Java我們用ECLIPSE; C#用Visual Studio)把從Selenium IDE經過錄制, 得到並轉化好的程序代碼在這些編譯器里運行, 運行的時候, 程序會調用Selenium RC來實現對剛才這段過程錄制的遠程操作 (可以控制打開一個firefox或者IE的瀏覽器進行回放, 也就是測試的過程.
簡單地說就是 Selenium可以用python語言來調用它(Selenium RC)進行測試.
不知道我說明白了沒有.
⑧ python selenium的文件下載在chrome、IE下如何設置並實現
設置下載目錄:
fromseleniumimportwebdriver
chromedriver="/usr/bin/chromedriver"
chromeOptions=webdriver.ChromeOptions()
prefs={"download.default_directory":"/path/download"}
chromeOptions.add_experimental_option("prefs",prefs)
driver=webdriver.Chrome(executable_path=chromedriver,chrome_options=chromeOptions)
設置代理:
fromseleniumimportwebdriver
PROXY="23.23.23.23:3128"#IP:PORTorHOST:PORT
chrome_options=webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=http://%s'%PROXY)
chrome=webdriver.Chrome(chrome_options=chrome_options)
chrome.get("明或虧http://whatismyipaddress.com")
IE的也差不多。
如果解決激神了您團族的問題請採納!
如果未解決請繼續追問