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 python ile ilgili bir sorun
6
●202
- 08-05-2023, 03:08:51alttaki 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.
- 08-05-2023, 03:12:03Kodunuzda proxy ve user-agent seçiminde rastgelelik kullanılmış ve bu da bazı durumlarda aynı proxy veya user-agent'ın kullanılmasına neden olabilir. Bunun yerine proxy ve user-agent listesinden sıradaki elemanı seçmek için bir döngü kullanabilirsiniz. Aşağıdaki kod, bu yöntemi kullanarak proxy ve user-agent seçimini düzenlemiştir:
from seleniumwire import webdriver import itertools 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() # create a cycle iterator for proxies and user-agents proxy_cycle = itertools.cycle(proxy_list) user_agent_cycle = itertools.cycle(user_agent_list) while True: # get the next proxy and user-agent proxy = next(proxy_cycle) user_agent = next(user_agent_cycle) # 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}" # 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() - 08-05-2023, 12:25:11teşekkür ederim. ama yine aynı. hala kendi bilgisayarımın user-agent'ini kullanıyor. yaklaşık 2500 tane user-agent var .txt dosyasında. galiba user-agent'i kullandıramıyorum hiç.Furknckmk adlı üyeden alıntı: mesajı görüntüle
- 09-05-2023, 00:31:37
chrome = webdriver.Chrome(seleniumwire_options=options)
yukarıdaki kodun olduğu kısmı aşağıdaki gibi değiştirin.
chrome_options = Options() chrome_options.add_argument("user-agent=" + user_agent) chrome = webdriver.Chrome(seleniumwire_options=options, chrome_options=chrome_options)ve aşağıdaki kütüphaneyi projenize dahil edin.
from selenium.webdriver.chrome.options import Options
- 09-05-2023, 01:30:40teşekkürler. ancak olmadı. projeyi başlatıyorum öylece bekliyor. hata vermiyor.Hayrettin adlı üyeden alıntı: mesajı görüntüle
- 09-05-2023, 16:29:47maalesef olmadı. herhangi bir hata vermiyor. bekliyor.Hayrettin adlı üyeden alıntı: mesajı görüntüle