我不知道为什么下面的代码将代理返回为无效,仅对Chrome浏览器的帮助表示赞赏。以下是进口。
import requests
import json
import time
import random
import threading
from threading import Thread
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from datetime import datetime
from proxymanager import ProxyManager
from random import randint
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.chrome.options import Options
def getProxy():
try:
proxy_manager = ProxyManager('proxies.txt')
proxydict = proxy_manager.random_proxy()
proxies = proxydict.get_dict()
except:
proxies = []
return proxies
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=https://%s' %getProxy)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")
答案 0 :(得分:1)
我会走出困境,猜测问题在于代理扩展 - 您是否尝试将dict
传递给Chrome而不是实际的代理地址。您希望从getProxy()
函数中的Proxy()类中获取实际值,例如:
def get_proxy(string_only=True):
try:
proxy_manager = ProxyManager("proxies.txt")
proxy = proxy_manager.random_proxy()
if string_only:
return proxy.proxy_string
return proxy.get_dict()
except (OSError, IOError, IndexError) as e: # couldn't load the file / file is empty
return None
# With Chrome:
chrome_options = webdriver.ChromeOptions()
proxy = get_proxy()
if proxy:
chrome_options.add_argument("--proxy-server=" + proxy)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")
# with requests:
response = requests.get("http://whatismyipaddress.com", proxies=get_proxy(False))
# etc.
如果您打算经常调用此函数并且proxies.txt
是静态文件,我还建议只加载代理列表一次。
答案 1 :(得分:0)
这有效
def get_proxy():
try:
proxy_manager = ProxyManager("proxies.txt")
return proxy_manager.random_proxy().proxy_string
except (OSError, IOError) as e: # couldn't load the file
return None
chrome_options = webdriver.ChromeOptions()
proxy = get_proxy()
if proxy:
chrome_options.add_argument("--proxy-server=" + proxy)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")