Home > Blockchain >  how to automatically change IP got from proxy api to use for selenium
how to automatically change IP got from proxy api to use for selenium

Time:12-03

from selenium import webdriver
from selenium.webdriver.common.proxy import *
from selenium.webdriver.common.by import By
from time import sleep
import requests

response = requests.get("http://proxy.tinsoftsv.com/api/changeProxy.php?key=mykey_apiG&location=0")
print(response.json())

proxy_url = "127.0.0.1:9009"
proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': proxy_url,
    'sslProxy': proxy_url,
    'noProxy': ''})

capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)

driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get("https://whoer.net/zh")

I am having a problem. I have rented a revolving proxy from a web service and I want to change the IP continuously via the api. I used the requests module to get a new IP, so how can it automatically get that new IP and replace it with the old one to use? I'm a newbie and really don't know much, hope someone can help me. Thanks very much!

I read the instructions of the service site but they don't have a tutorial for python

CodePudding user response:

The code appears to be correctly importing the necessary modules and using them to create a Proxy object and a webdriver.Chrome object.

However, there are a few issues with the code that may cause it to not work as expected:

The proxy_url variable is set to "127.0.0.1:9009", which is the localhost IP address and port number. This is not a valid proxy server, and it will not allow the webdriver.Chrome object to access the internet. You should replace this with a valid proxy server IP address and port number.

The response variable is not being used in the code. The requests.get() method is used to get a response from the proxy API, but the response is not being saved or used in any way. You should either use the response to get a valid proxy server IP address and port number, or remove the requests.get() method from the code.

The sleep() method is imported but not used in the code. This is not causing any errors, but it is unnecessary and can be removed from the code.

Here is the corrected code:

from selenium import webdriver
from selenium.webdriver.common.proxy import *
from selenium.webdriver.common.by import By
import requests

response = requests.get("http://proxy.tinsoftsv.com/api/changeProxy.php?key=mykey_apiG&location=0")
proxy_url = response.json()["ip"]

proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': proxy_url,
    'sslProxy': proxy_url,
    'noProxy': ''})

capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)

driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get("https://whoer.net/zh")

CodePudding user response:

To change the IP address used by your Selenium webdriver, you can do the following:

from selenium import webdriver
from selenium.webdriver.common.proxy import *
from selenium.webdriver.common.by import By
from time import sleep
import requests

# Get the latest IP address from the proxy service
response = requests.get("http://proxy.tinsoftsv.com/api/changeProxy.php?key=mykey_apiG&location=0")
ip = response.json()["ip"]
port = response.json()["port"]

# Update the proxy URL with the new IP address
proxy_url = f"{ip}:{port}"

# Create a proxy object with the updated proxy URL
proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': proxy_url,
    'sslProxy': proxy_url,
    'noProxy': ''})

# Update the capabilities object to use the new proxy
capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)

# Create a new instance of the webdriver with the updated capabilities
driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get("https://whoer.net/zh")
  • Related