Python 代理功能适用于请求,但不适用于chrome浏览器?
我不知道为什么下面的代码将代理返回为无效,只有感谢chrome浏览器的帮助。以下是进口商品Python 代理功能适用于请求,但不适用于chrome浏览器?,python,Python,我不知道为什么下面的代码将代理返回为无效,只有感谢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.
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")
我想问题出在代理扩展上——您试图将
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
是一个静态文件,我还建议只加载一次代理列表。这很有效
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")
尝试使用http://aswell这似乎只是返回我的IP,而不是proxy@JustACoder-然后您的
proxies.txt
已将您的IP定义为代理,请检查其内容。在传递给Chrome之前,您始终可以打印/跟踪代理的值,以确保它选择了正确的值。为什么要这样做?(将我的IP定义为代理)proxy=get_proxy()也给了我一个错误,该错误表示协助返回的函数调用None@JustACoder-为什么它会将您的地址定义为代理,我不知道-我没有创建您的proxies.txt
。你确定它已经上膛了吗?至于这个错误,你能发布一个完整的跟踪吗?