Python Selenium Webdriver-代理:查询参数

Python Selenium Webdriver-代理:查询参数,python,selenium-webdriver,proxy,Python,Selenium Webdriver,Proxy,我看到的挑战是,通过selenium,我试图点击一个网站元素(一个带有一些js的div)。“按钮”可将您导航到另一页 如何配置浏览器以通过代理自动路由请求 我的代理设置如下: http://api.myproxy.com?key=AAA111BBB6&url=http://awebsitetobrowse.com 我试图将webdriver(chrome)置于代理之后 from selenium import webdriver options = webdriver.ChromeOpti

我看到的挑战是,通过selenium,我试图点击一个网站元素(一个带有一些js的
div
)。“按钮”可将您导航到另一页

如何配置浏览器以通过代理自动路由请求

我的代理设置如下:
http://api.myproxy.com?key=AAA111BBB6&url=http://awebsitetobrowse.com

我试图将webdriver(chrome)置于代理之后

from selenium import webdriver   
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(chrome_options=options)
其中,到目前为止,选项是浏览器窗口大小的一些基本配置

我见过不少例子(,),但不知何故,我找不到一个适合我需要的例子



虽然您使用的代理地址似乎不是实际的代理,但它是一个API,在处理代理、验证码或任何IP阻塞后返回页面本身的HTML内容。但对于不同的场景,可能会有不同的解决方案。其中一些如下

情景1

因此,据我所知,如果您的 api提供了通过代理返回已访问页面的响应的工具

因此,它应该直接用在“driver.get()”中 地址=”http://api.scraperapi.com/?api_key=YOURAPIKEY&url=“+url通过api访问

这方面的示例代码如下所示:

import os
dir_path = os.path.dirname(os.path.realpath(__file__)) + "\\chromedriver.exe"
APIKEY=1234 #replace with your API Key
apiURL = "http://api.scraperapi.com/?api_key="+APIKEY+"&render=true&url="

visit_url = "https://stackoverflow.com/questions/11450158/how-do-i-set-proxy-for-chrome-in-python-webdriver"

from selenium import webdriver
driver = webdriver.Chrome(executable_path = dir_path)
driver.get(apiURL+visit_url)
import os 
dir_path = os.path.dirname(os.path.realpath(__file__)) + "\\chromedriver.exe"
import requests
proxyapi = "http://api.scraperapi.com?api_key=1234&render=true" 
proxy=requests.get(proxyapi).text

visit_url = "https://stackoverflow.com/questions/11450158/how-do-i-set-proxy-for-chrome-in-python-webdriver"

from selenium import webdriver   
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server='+proxy)
driver = webdriver.Chrome(executable_path = dir_path, chrome_options=chrome_options)
driver.get(visit_url)
import os 
dir_path = os.path.dirname(os.path.realpath(__file__)) + "\\chromedriver.exe"
proxyapi = "http://api.scraperapi.com?api_key=1234&render=true" 

visit_url = "https://stackoverflow.com/questions/11450158/how-do-i-set-proxy-for-chrome-in-python-webdriver"

from selenium import webdriver   
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server='+proxyapi)
driver = webdriver.Chrome(executable_path = dir_path, chrome_options=chrome_options)
driver.get(visit_url)
场景2

但如果您有一些API提供代理地址和登录 凭据作为响应,然后可以在chrome选项中伪造以供使用 它是用铬本身制作的

若api的响应类似于

  • “议定书://用户:password@proxyserver:proxyport“(在进行身份验证时)
  • “PROTOCOL://proxyserver:proxyport”(如果是空身份验证)
在这两种情况下,协议可以像HTTP、HTTPS、SOCKS4、SOCKS5等

代码应该是这样的:

import os
dir_path = os.path.dirname(os.path.realpath(__file__)) + "\\chromedriver.exe"
APIKEY=1234 #replace with your API Key
apiURL = "http://api.scraperapi.com/?api_key="+APIKEY+"&render=true&url="

visit_url = "https://stackoverflow.com/questions/11450158/how-do-i-set-proxy-for-chrome-in-python-webdriver"

from selenium import webdriver
driver = webdriver.Chrome(executable_path = dir_path)
driver.get(apiURL+visit_url)
import os 
dir_path = os.path.dirname(os.path.realpath(__file__)) + "\\chromedriver.exe"
import requests
proxyapi = "http://api.scraperapi.com?api_key=1234&render=true" 
proxy=requests.get(proxyapi).text

visit_url = "https://stackoverflow.com/questions/11450158/how-do-i-set-proxy-for-chrome-in-python-webdriver"

from selenium import webdriver   
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server='+proxy)
driver = webdriver.Chrome(executable_path = dir_path, chrome_options=chrome_options)
driver.get(visit_url)
import os 
dir_path = os.path.dirname(os.path.realpath(__file__)) + "\\chromedriver.exe"
proxyapi = "http://api.scraperapi.com?api_key=1234&render=true" 

visit_url = "https://stackoverflow.com/questions/11450158/how-do-i-set-proxy-for-chrome-in-python-webdriver"

from selenium import webdriver   
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server='+proxyapi)
driver = webdriver.Chrome(executable_path = dir_path, chrome_options=chrome_options)
driver.get(visit_url)
场景3

但是,如果您有一些API本身是一个具有空身份验证的代理,那么可以在chrome选项中使用它 它是用铬本身制作的

代码应该是这样的:

import os
dir_path = os.path.dirname(os.path.realpath(__file__)) + "\\chromedriver.exe"
APIKEY=1234 #replace with your API Key
apiURL = "http://api.scraperapi.com/?api_key="+APIKEY+"&render=true&url="

visit_url = "https://stackoverflow.com/questions/11450158/how-do-i-set-proxy-for-chrome-in-python-webdriver"

from selenium import webdriver
driver = webdriver.Chrome(executable_path = dir_path)
driver.get(apiURL+visit_url)
import os 
dir_path = os.path.dirname(os.path.realpath(__file__)) + "\\chromedriver.exe"
import requests
proxyapi = "http://api.scraperapi.com?api_key=1234&render=true" 
proxy=requests.get(proxyapi).text

visit_url = "https://stackoverflow.com/questions/11450158/how-do-i-set-proxy-for-chrome-in-python-webdriver"

from selenium import webdriver   
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server='+proxy)
driver = webdriver.Chrome(executable_path = dir_path, chrome_options=chrome_options)
driver.get(visit_url)
import os 
dir_path = os.path.dirname(os.path.realpath(__file__)) + "\\chromedriver.exe"
proxyapi = "http://api.scraperapi.com?api_key=1234&render=true" 

visit_url = "https://stackoverflow.com/questions/11450158/how-do-i-set-proxy-for-chrome-in-python-webdriver"

from selenium import webdriver   
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server='+proxyapi)
driver = webdriver.Chrome(executable_path = dir_path, chrome_options=chrome_options)
driver.get(visit_url)

因此,该解决方案可以根据不同的场景使用。

好吧,经过无数次的实验,我发现该方案适用于:

apiURL = "http://api.scraperapi.com/?api_key="+APIKEY+"&render=true&url="
而惨败与

apiURL = "http://api.scraperapi.com?api_key="+APIKEY+"&render=true&url="

我不得不承认我的无知:我认为这两者应该是等价的

有可能使它更具可复制性吗?例如,你能分享这个代理提供商的名字吗?谢谢。我正在使用。你能描述一下当你使用的例子时它是如何失败的吗?它没有失败。我找不到一个可以将webdriver配置为生成上述请求的示例。我只能看到
chrome\u选项。添加参数('--proxy server=http://%s'%proxy)
其中有一个固定的http地址和端口。在我的例子中,如何将
&url=something
组合在一起,其中
something
是我想要查看的页面?这不起作用,违背了整个目的。当你点击页面上的某些元素触发到另一个页面的导航(JS)时,Chrome需要将请求连接到引擎盖下的代理。这和我在回答中说的不一样吗?好的,我在给你的示例代码中漏掉了“/”,还有一件事,你成功地将这个API用作代理了吗?Avinash,您应该使用chrome或firefox选项设置代理,而不是手动伪造url。如前所述,如果不通过浏览器选项设置代理,则不会将请求(例如:js路由)路由到代理。不过谢谢你的帮助。