selenium之chrome浏览器设置userAgent和代理ip
1.设置user-agent
option = ChromeOptions()#设置无头模式option.add_argument("--headless")option.add_experimental_option('excludeSwitches', ['enable-automation'])option.add_experimental_option('useAutomationExtension', False)#设置user-agentoption.add_argument('user-agent=ywy')driver = webdriver.Chrome(options=option)
2.通过user-agent池替换user-agent
pip install fake_useragentfrom fake_useragent import UserAgentheaders = {
‘User-Agent’: UserAgent().random,
}
3.设置代理ip
3.1 连接无用户名密码认证的代理
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--proxy-server=http://ip:port')
driver = webdriver.Chrome(chrome_options=chromeOptions)
3.2有用户名和密码的连接
from selenium import webdriver
def create_proxyauth_extension(proxy_host, proxy_port,proxy_username, proxy_password,scheme='http', plugin_path=None):"""Proxy Auth Extensionargs:proxy_host (str): domain or ip address, ie proxy.domain.comproxy_port (int): portproxy_username (str): auth usernameproxy_password (str): auth passwordkwargs:scheme (str): proxy scheme, default httpplugin_path (str): absolute path of the extension return str -> plugin_path"""import stringimport zipfileif plugin_path is None:plugin_path = 'd:/webdriver/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_pathproxyauth_plugin_path = create_proxyauth_extension(proxy_host="proxy.crawlera.com",proxy_port=8010,proxy_username="username",proxy_password="password"
)co = webdriver.ChromeOptions()
co.add_argument("--start-maximized")
co.add_extension(proxyauth_plugin_path)driver = webdriver.Chrome(chrome_options=co)
driver.get("http://www.amazon.com/")
4.Selenium chrome配置不加载图片
prefs = {"profile.managed_default_content_settings.images":2}
chrome_options.add_experimental_option("prefs",prefs)
selenium之chrome浏览器设置userAgent和代理ip相关推荐
- scrapy基础补齐,python中设置User-agent和代理ip
常见的补齐方法:Python爬虫:关于scrapy模块的请求头_彭世瑜的技术博客_51CTO博客 [1]设置浏览器头的python基础实现函数是 urllib.request.Request() cl ...
- java中selenium设置chrome浏览器为开发者模式与IP代理
java中selenium设置chrome浏览器为开发者模式与IP代理 前言 代码 前言 之前为公司写了个爬虫项目,有时候为了绕过网站的反爬机制需要用到ip代理以及设置浏览器为开发者模式,看了一些se ...
- Selenium之Chrome浏览器的启动问题及解决
Selenium之Chrome浏览器的启动问题及解决 参考文章: (1)Selenium之Chrome浏览器的启动问题及解决 (2)https://www.cnblogs.com/biyuting/p ...
- Chrome浏览器设置打开书签时在新标签页打开(保姆级图文)
步骤 系列文章 前言 1.快捷键 2.网上的Neat Bookmarks拓展 总结 系列文章 提示:转到日常小技巧专栏,观看更多内容! 点我直达–>日常小技巧专栏 前言 Chrome浏览器设置打 ...
- Chrome浏览器设置护眼模式(离线安装插件)
Chrome浏览器设置护眼模式(离线安装插件) 1.下载眼睛护航Care-your-Eyes_v5.1.3.crx插件 https://pan.baidu.com/s/1Xahnt8NQfDxaKid ...
- 给chrome浏览器设置壁纸
chrome浏览器设置壁纸 很多人的chrome浏览器设置的搜索引擎都是百度,这样的话就不能给浏览器本身设置背景了,chrome官方禁止. 一. 首先下载stylus插件 下载地址: https:// ...
- chrome浏览器设置cookie失效
chrome浏览器设置cookie失效 复现步骤 在https协议下登录页面,成功设置cookie.这个时候把https协议换成http,再重新登录,发现登录不了cookie没有设置成功. 原因 项目 ...
- Google Chrome浏览器设置默认应用出现两个Google Chrome,及设置默认浏览器打不开第三方应用内链接
一.Chrome浏览器设置默认应用出现两个Chrome 1.Windows键+R键打开运行 2.输入regedit打开注册表编辑器 3.搜索 计算机\HKEY_CLASSES_ROOT\ChromeH ...
- Chrome浏览器如何使用socket5代理?
Chrome浏览器如何使用socket5代理? 转载:http://helpserver.chiwudaili.com/detail.aspx?id=60222b96a3240a1376eef8a70 ...
最新文章
- 报错解决:InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got
- 注解 java.lang.annotation.Inherited 介绍
- qt怎么做滑动调节参数_冬天冰箱温度怎么调?0到7旋钮是做什么的?学会调节省电又保鲜...
- leetcode 752. 打开转盘锁 c代码
- oracle sql判断相等,Oracle PL/SQL判断两个字段相等或不等问题
- hadoop2.6.5安装文档及解决root用户无法ssh localhost的问题
- H3C TFTP操作示例
- Vue面试题 70道题目及答案
- python热键+鼠标键盘控制
- Path variable [contentHash:8] not implemented in this context: styles.[contentHash:8].css
- Android2.2 API 中文文档
- 温湿度传感器实验-传感器原理及应用实验
- 怎么样抓取微信小程序
- C语言:链表(动态)创建之头插法和尾插法
- 【图解】共模干扰,差模干扰
- Hive SQL 使用不了union all : SemanticException Cartesian products are disabled for safety reasons
- 数据分析-Jupyter,Matplotlib介绍-Matplotlib绘制图形
- eclipse的jsp第一行代码报错_Eclipse手记(小弟发现最实用的资料之一)-JSP教程,Java技巧及代码...
- 解决pyecharts在pycharm不显示图片
- 网络速率方面的知识(Kbps Mbps KB/s Mb/s Kb/s等)