alttaki kodda txt'lerden proxy ve user-agent çekmek istiyorum. example.com girip 10 saniye bekleyip, sonra sekmeyi kapatıp, yeni proxy ve user-agentle devam etmesini istiyorum. ancak user-agent hiç değişmiyor. sizce sorun nedir? proxy'de değişmiyor olabilir. elimdekiler rotating olabilir.
from seleniumwire import webdriver
import random
import time
# read proxy list from file
with open('proxy.txt', 'r') as f:
proxy_list = f.read().splitlines()
# read user-agent list from file
with open('user.txt', 'r') as f:
user_agent_list = f.read().splitlines()
# initialize used proxies and user-agents
used_proxies = []
used_user_agents = []
# initialize user-agent index to 0
user_agent_index = 0
while True:
# select a random proxy
proxy = random.choice(proxy_list)
# check if proxy has been used before
if proxy in used_proxies:
continue
# add proxy to used list
used_proxies.append(proxy)
# parse proxy information
proxy_info = proxy.split(':')
if len(proxy_info) == 4:
# if username and password are included in the proxy string
username = proxy_info[2]
password = proxy_info[3]
proxy_url = f"http://{username}:{password}@{proxy_info[0]}:{proxy_info[1]}"
else:
# if no username and password are included in the proxy string
proxy_url = f"http://{proxy}"
# choose a random user-agent
user_agent = random.choice(user_agent_list)
# create Chrome driver with selected proxy and user-agent
options = {
'proxy': {
'http': proxy_url,
'https': proxy_url,
'no_proxy': 'localhost,127.0.0.1' # exclude localhost from proxy
},
'headers': {
'User-Agent': user_agent
}
}
chrome = webdriver.Chrome(seleniumwire_options=options)
chrome.get("https://www.example.com")
time.sleep(10)
# do whatever you need to do with the Chrome driver here
# quit the driver
chrome.quit()
# remove used proxy from used list
used_proxies.remove(proxy)
# increment user-agent index to get the next user-agent in the list
user_agent_index += 1
# if we have used all the user-agents in the list, start over from the beginning
if user_agent_index >= len(user_agent_list):
user_agent_index = 0