Skip to content

Instantly share code, notes, and snippets.

@tushortz
Last active April 3, 2024 15:35
Show Gist options
  • Star 78 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save tushortz/cba8b25f9d80f584f807b65890f37be5 to your computer and use it in GitHub Desktop.
Save tushortz/cba8b25f9d80f584f807b65890f37be5 to your computer and use it in GitHub Desktop.
from selenium import webdriver
from selenium.webdriver.chrome.options import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy, ProxyType
import time
co = webdriver.ChromeOptions()
co.add_argument("log-level=3")
co.add_argument("--headless")
def get_proxies(co=co):
driver = webdriver.Chrome(chrome_options=co)
driver.get("https://free-proxy-list.net/")
PROXIES = []
proxies = driver.find_elements_by_css_selector("tr[role='row']")
for p in proxies:
result = p.text.split(" ")
if result[-1] == "yes":
PROXIES.append(result[0]+":"+result[1])
driver.close()
return PROXIES
ALL_PROXIES = get_proxies()
def proxy_driver(PROXIES, co=co):
prox = Proxy()
if len(PROXIES) < 1:
print("--- Proxies used up (%s)" % len(PROXIES))
PROXIES = get_proxies()
pxy = PROXIES[-1]
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = pxy
prox.socks_proxy = pxy
prox.ssl_proxy = pxy
capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)
driver = webdriver.Chrome(chrome_options=co, desired_capabilities=capabilities)
return driver
# --- YOU ONLY NEED TO CARE FROM THIS LINE ---
# creating new driver to use proxy
pd = proxy_driver(ALL_PROXIES)
# code must be in a while loop with a try to keep trying with different proxies
running = True
while running:
try:
mycodehere()
# if statement to terminate loop if code working properly
# you need to modify condition_met
if condition_met:
running = False
# you
except:
new = ALL_PROXIES.pop()
# reassign driver if fail to switch proxy
pd = proxy_driver(ALL_PROXIES)
print("--- Switched proxy to: %s" % new)
time.sleep(1)
@demetranadya
Copy link

Во взятии прокси с сайта косяк.
-- Proxies used up (0)
['IP', 'Address', 'Port', 'Code', 'Country', 'Anonymity', 'Google', 'Https', 'Last', 'Checked']
['193.30.243.175', '8080', 'UA', 'Ukraine', 'elite', 'proxy', 'no', 'yes', '20', 'seconds', 'ago']
['203.189.149.132', '38157', 'KH', 'Cambodia', 'elite', 'proxy', 'no', 'yes', '20', 'seconds', 'ago']

@demetranadya
Copy link

trs = driver.find_elements_by_css_selector("tr[role='row']")
for tr in trs:
tds = tr.find_elements_by_css_selector("td")
if tds[-2].text.split() == "yes":
PROXIES.append(tds[0].text.split()+":"+tds[0].text.split()[1])

@ajaiau
Copy link

ajaiau commented Nov 13, 2018

--- Proxies used up (0)
Traceback (most recent call last):
File "rotate_proxy.py", line 56, in
pd = proxy_driver(ALL_PROXIES)
File "rotate_proxy.py", line 41, in proxy_driver
prox.http_proxy = pxy
UnboundLocalError: local variable 'pxy' referenced before assignment

@stefan-djurovic
Copy link

Traceback (most recent call last):
File "test_v1.py", line 53, in
pd = proxy_driver(ALL_PROXIES)
File "test_v1.py", line 47, in proxy_driver
driver = webdriver.Chrome('/home/djurovic/Desktop/Linux ChromeDriver/chromedriver', chrome_options=co, desired_capabilities=capabilities)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/chrome/webdriver.py", line 81, in init
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in init
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: proxy
from invalid argument: Specifying 'socksProxy' requires an integer for 'socksVersion'
(Driver info: chromedriver=2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),platform=Linux 4.15.0-43-generic x86_64)

@stefan-djurovic
Copy link

To fix erros as mine, just downgrade your chrome webdriver, it worked on 2.41

@michaelfresco
Copy link

@ervlh
Copy link

ervlh commented Jun 13, 2019

still has this error on ChromeDriver 75.0.3770.8

To fix erros as mine, just downgrade your chrome webdriver, it worked on 2.41

@kirill50
Copy link

I found this page useful to fix the script: https://stackoverflow.com/questions/50662935/python3-cant-set-socks-proxy-with-chromedriver-socks-py-type-integer-error

But can you please post the fixed version.

@tejasbirsingh
Copy link

tejasbirsingh commented Jun 26, 2020

This is the working script of the above script . I removed prox.socks_proxy = pxy and it started working as intended . Thanks !

from selenium import webdriver
from selenium.webdriver.chrome.options import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy, ProxyType

import time

# Add path to your WebDriver according to the browser you are using 
PATH = "E:\SeleniumProject\chromedriver.exe"	

co = webdriver.ChromeOptions()
co.add_argument("log-level=3")
co.add_argument("--headless")

def get_proxies(co=co):
    driver = webdriver.Chrome(options=co)
    driver.get("https://free-proxy-list.net/")

    PROXIES = []
    proxies = driver.find_elements_by_css_selector("tr[role='row']")
    for p in proxies:
        result = p.text.split(" ")

        if result[-1] == "yes":
            PROXIES.append(result[0]+":"+result[1])

    driver.close()
    return PROXIES


ALL_PROXIES = get_proxies()


def proxy_driver(PROXIES, co=co):
    prox = Proxy()

    if PROXIES:
        pxy = PROXIES[-1]
    else:
        print("--- Proxies used up (%s)" % len(PROXIES))
        PROXIES = get_proxies()

    prox.proxy_type = ProxyType.MANUAL
    prox.http_proxy = pxy
  
    prox.ssl_proxy = pxy

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

    driver = webdriver.Chrome(options=co, desired_capabilities=capabilities)

    return driver



# --- YOU ONLY NEED TO CARE FROM THIS LINE ---
# creating new driver to use proxy
pd = proxy_driver(ALL_PROXIES)

# code must be in a while loop with a try to keep trying with different proxies
running = True

while running:
    try:
        mycodehere()
        
        # if statement to terminate loop if code working properly
        something()
        
        # you 
    except:
        new = ALL_PROXIES.pop()
        
        # reassign driver if fail to switch proxy
        pd = proxy_driver(ALL_PROXIES)
        print("--- Switched proxy to: %s" % new)
        time.sleep(1)

@tushortz
Copy link
Author

tushortz commented Jun 26, 2020

i've updated the code. Sorry guys didn't even know that this script was being used

@tejasbirsingh
Copy link

tejasbirsingh commented Jun 26, 2020

tushortz can you please help me add sms bomber code into your code which changes its proxies . I tried but its not working !
Thanks!

from selenium import webdriver
from selenium.webdriver.chrome.options import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy, ProxyType

import time

PATH = "E:\SeleniumProject\chromedriver.exe"

co = webdriver.ChromeOptions()
co.add_argument("log-level=3")
co.add_argument("--headless")

def get_proxies(co=co):
driver = webdriver.Chrome(options=co)
driver.get("https://free-proxy-list.net/")

PROXIES = []
proxies = driver.find_elements_by_css_selector("tr[role='row']")
for p in proxies:
	result = p.text.split(" ")

	if result[-1] == "yes":
		PROXIES.append(result[0]+":"+result[1])

driver.close()
# print(PROXIES)
return PROXIES

ALL_PROXIES = get_proxies()

def proxy_driver(PROXIES, co=co):
prox = Proxy()

if PROXIES:
	pxy = PROXIES[-1]
else:
	print("--- Proxies used up (%s)" % len(PROXIES))
	PROXIES = get_proxies()

prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = pxy
prox.ssl_proxy = pxy

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

driver = webdriver.Chrome(options=co, desired_capabilities=capabilities)

return driver

def smsBomber():
browser1 = proxy_driver(ALL_PROXIES)

		mobile_number = "6239027206"

		frequency = 2
		for i in range(frequency):
			# try:
			# 	browser1.get('https://www.flipkart.com/account/login?ret =/')
			# 	number = browser1.find_element_by_class_name('_2zrpKA')

			# 	number.send_keys(mobile_number)
			# 	forgot = browser1.find_element_by_link_text('Forgot?')
			# 	forgot.click()
			# 	time.sleep(3)
			# except:
			# 	pass

			try:
				browser1.get('https://www.oyorooms.com/login?country=&retUrl=/')
				number = browser2.find_element_by_class_name('textTelInput__input')
				number.send_keys(mobile_number)
				verify = browser2.find_element_by_class_name('loginCard__button')
				verify.click()
				time.sleep(3)

			except:
				pass

running = True

while running:
try:

	smsBomber(browser1)
	browser1.quit()
	
except:
	new = ALL_PROXIES.pop()	

	pd = proxy_driver(ALL_PROXIES)

@bguan2020
Copy link

should i replace all instances of my own "driver" with "pd"?

@brakisto
Copy link

brakisto commented Dec 7, 2021

co = webdriver.ChromeOptions()
co.add_argument("log-level=3")
co.add_argument("--headless")

def get_proxies(co=co):
driver = webdriver.Chrome(chrome_options=co,executable_path=ChromeDriverManager().install())
driver.get("https://free-proxy-list.net/")

PROXIES = []
trs = driver.find_elements_by_tag_name("tr")
for tr in trs:
    tds = tr.find_elements_by_tag_name("td")
    if tds[-2].text == "yes":
        PROXIES.append(tds[0].text+":"+tds[1].text)
        print(PROXIES)
driver.close()
return PROXIES

ALL_PROXIES = get_proxies()

print(ALL_PROXIES)

def proxy_driver(PROXIES, co=co):
prox = Proxy()

if len(PROXIES) < 1:
    print("--- Proxies used up (%s)" % len(PROXIES))
    PROXIES = get_proxies()

print(PROXIES)
pxy = PROXIES[-1]

prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = pxy
prox.ssl_proxy = pxy

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

driver = webdriver.Chrome(chrome_options=co, desired_capabilities=capabilities,executable_path=ChromeDriverManager().install())

return driver

This works for me guys, thanks tushortz for the script!

@tushortz
Copy link
Author

tushortz commented Dec 7, 2021

glad it helped

@SIMPNDEV
Copy link

Why don't you try like the following way instead:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

import time
import requests
from bs4 import BeautifulSoup

link = 'https://stackoverflow.com/questions/tagged/web-scraping'

def filter_proxies():   
    response = requests.get('https://www.sslproxies.org/')
    soup = BeautifulSoup(response.text,"html.parser")
    proxies = []
    for item in soup.select("table.table tbody tr"):
        if not item.select_one("td"):break
        ip = item.select_one("td").text
        port = item.select_one("td:nth-of-type(2)").text
        proxies.append(f"{ip}:{port}")
    return proxies


def create_proxy_driver(PROXY):
    options = Options()
    options.add_argument("--headless")
    options.add_argument(f'--proxy-server={PROXY}')
    driver = webdriver.Chrome(options=options)
    return driver


def get_content(ALL_PROXIES,driver):
    while True:
        try:
            driver.get(link)
            title = driver.find_element(By.CSS_SELECTOR,"h3.s-post-summary--content-title > a").text
            if title:
                driver.quit()
                return title
        except Exception as e:
            driver.quit()
            if not ALL_PROXIES:
                print("Proxies used up (%s)" % len(ALL_PROXIES))
                ALL_PROXIES = filter_proxies()

            new_proxy = ALL_PROXIES.pop()
            driver = create_proxy_driver(new_proxy)
            print("proxy being used: %s" % new_proxy)
            time.sleep(1)


if __name__ == '__main__':
    ALL_PROXIES = filter_proxies()
    new_proxy = ALL_PROXIES.pop()
    driver = create_proxy_driver(new_proxy)
    print(get_content(ALL_PROXIES,driver))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment