python学习-Selenium爬虫之使用代理ip的方法
2020-03-12 12:11
519 查看
今天给大家分享的是如何在爬取数据的时候防止 IP被封,今天给大家分享两种方法,希望大家可以认真学习,再也不用担心被封IP啦。
第一种:
降低访问速度,我们可以使用 time模块中的sleep,使程序每运行一次后就睡眠1s,这样的话就可以大大的减少ip被封的几率
第二种:
为了提高效率,我们可以使用代理 ip来解决,ip是亿牛云的动态转发代理,以下是代理配置过程的示例
Selenium ¶
from selenium import webdriver
import string import zipfile # 代理服务器 proxyHost = "t.16yun.cn" proxyPort = "31111" # 代理隧道验证信息 proxyUser = "username" proxyPass = "password" def create_proxy_auth_extension (proxy_host, proxy_port, proxy_username, proxy_password, scheme= 'http' , plugin_path= None ): if plugin_path is None : plugin_path = r 'C:/{}_{}@t.16yun.zip' .format(proxy_username, proxy_password) manifest_json = """ { "version": "1.0.0", "manifest_version": 2, "name": "16YUN Proxy", "permissions": [ "proxy", "tabs", "unlimitedStorage", "storage", "", "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: [""]}, ['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 proxy_auth_plugin_path = create_proxy_auth_extension( proxy_host=proxyHost, proxy_port=proxyPort, proxy_username=proxyUser, proxy_password=proxyPass) option = webdriver.ChromeOptions() option.add_argument( "--start-maximized" ) # 如报错 chrome-extensions # option.add_argument("--disable-extensions") option.add_extension(proxy_auth_plugin_path) driver = webdriver.Chrome(chrome_options=option) driver.get( "http://httpbin.org/ip" )
好 了,今天关于 python学习的分享就到这里,上边的那段代码可以直接使用,但是里边的代理应该已经过期,大家在使用的时候可能需要联系代理商开通服务,最后呢希望大家能够收藏起来,要记得做笔记哦。好记性不如烂笔头。
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 【Python3.6爬虫学习记录】(六)urllib详细使用方法(header,代理,超时,认证,异常处理)
- 【Python3.6爬虫学习记录】(十一)使用代理IP及用多线程测试IP可用性--刷访问量
- selenium+python设置爬虫代理IP的方法
- python selenium 爬虫,使用代理 ,和 python 爬取代理网站,保存ip
- selenium+python设置爬虫代理IP的方法
- Selenium爬虫之使用代理ip的方法
- python selenium模块Chrome设置代理ip的方法
- python使用IP代理示例及出错解决方法
- Python爬虫使用代理IP的实现
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍
- Python爬虫学习(9):Selenium的使用
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍
- Python:爬虫使用代理防封IP:HTTP Error 403: Forbidden
- Python 爬虫入门(二)—— IP代理使用 - 不剃头的一休哥 - 博客园
- python爬虫,selenium使用,Firefox-chrome-IE问题解决方法,selenium的基本操作
- Python爬虫设置代理IP和伪装成浏览器的方法
- Python3 爬虫中代理的使用方法
- Python实现爬虫设置代理IP和伪装成浏览器的方法分享
- Python代理IP爬虫的新手使用教程
- python爬虫防止IP被封的一些措施(伪造User-Agent ,在每次重复爬取之间设置一个随机时间间隔 ,伪造cookies ,使用代理)