Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编辑函数内的代理变量。Python硒铬转换器_Python_Selenium_Selenium Webdriver_Proxy_Selenium Chromedriver - Fatal编程技术网

编辑函数内的代理变量。Python硒铬转换器

编辑函数内的代理变量。Python硒铬转换器,python,selenium,selenium-webdriver,proxy,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Proxy,Selenium Chromedriver,我将此代码改编为我的脚本 我不知道如何使用从我的文件中检索到的代理启动Chromedriver 首先,我提取一行ip:port:user:pass格式的代理 def proxychoose(): lines = open('proxy.txt').readlines(1) proxyline = ''.join(lines) with open("proxy.txt", "r") as f: lines = f.readlines() with

我将此代码改编为我的脚本

我不知道如何使用从我的文件中检索到的代理启动Chromedriver

首先,我提取一行ip:port:user:pass格式的代理

def proxychoose():
lines = open('proxy.txt').readlines(1)
proxyline = ''.join(lines)

with open("proxy.txt", "r") as f:
    lines = f.readlines()
with open("proxy.txt", "w") as f:
    for line in lines:
        if line.strip("\n") != proxyline.strip() :
            f.write(line)
print(proxyline)
return proxyline
然后我分割这行,为变量提供新的值

proxyline=proxychoose()

PROXY_HOST = proxyline.split(":")[0]
PROXY_PORT = proxyline.split(":")[1]
PROXY_USER = proxyline.split(":")[2]
PROXY_PASS = proxyline.split(":")[3]
最后,我使用上面网站的代码启动驱动程序

path = os.path.dirname(os.path.abspath(__file__))
opts = webdriver.ChromeOptions()
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
    zp.writestr("manifest.json", manifest_json)
    zp.writestr("background.js", background_js)
opts.add_extension(pluginfile)
driver = webdriver.Chrome(
    os.path.join(path, 'chromedriver'),
    options=opts)
driver.get('https://httpbin.org/ip')
完整代码