Selenium爬虫之使用代理ip的方法

[复制链接]
629|3
手机看帖
扫描二维码
随时随地手机跟帖
yiniuyun|  楼主 | 2019-3-27 17:22 | 显示全部楼层 |阅读模式

我们在爬取数据的时候有可能会出现爬到一半就不动了,有可能是因为目标网站封了你的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")
    option.add_extension(proxy_auth_plugin_path)

    driver = webdriver.Chrome(chrome_options=option)

driver.get("http://httpbin.org/ip")
这段代码示例是可以直接复制使用的,但是里面的代理信息是我使用过期了的,所以要程序可以运行,还需要在他们客服那里开通代理获取新的代理信息替换才可以使用。

使用特权

评论回复

相关帖子

零三翟邢止胃| | 2019-3-28 08:51 | 显示全部楼层
好帖子啊! 不顶,对不起啊!多谢分享啊! 感谢啊!很好的啊! 不错的啊! 赞一个啊!

使用特权

评论回复
无语画眉| | 2019-3-28 09:23 | 显示全部楼层

好帖子啊!

使用特权

评论回复
逆天凡凡| | 2019-3-28 12:57 | 显示全部楼层
感谢分享

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

7

主题

7

帖子

0

粉丝