python - 使用映射列表进行多线程处理时获取列表中的下一项
问题描述
我正在使用代理来访问url
. 由于这是代理列表,因此某些代理可能不起作用。如果连接失败,有没有办法选择下一个代理?我不明白,如何在使用concurrent.futures
映射时做到这一点。
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import concurrent.futures
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
proxy_page = requests.get('https://advanced.name/proxy/fe33f5f3990569edd7fed5a076cfad75').text
with open('proxies.txt', 'w', encoding='utf-8') as f:
f.write(proxy_page)
with open('proxies.txt') as f:
proxy_list = list(f)
links=['https://www.snapchat.com/add/co.ducks',
'https://www.snapchat.com/add/rebekkaoulie',
'https://www.snapchat.com/add/derrengt',
'https://www.snapchat.com/add/vaumurtch',
'https://www.snapchat.com/add/akseltrefall']
i_list = [1,2,3,4,5]
def get_screenshot(link, i, proxy):
_start = time.time()
options = Options() # Runs Chrome in headless mode.
'''options.add_argument('--headless')'''
options.add_argument('--no-sandbox') # # Bypass OS security model
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--proxy-server={}'.format(proxy))
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(options=options, executable_path='C:/chromedriver.exe')
driver.get(link)
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By. CLASS_NAME,'css-1motqmv')))
driver.save_screenshot(f'screenshot-headless{i}.png')
driver.quit()
_end = time.time()
i += 1
print('Total time for headless {}'.format(_end - _start))
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
results = executor.map(get_screenshot, links, i_list, proxy_list)
解决方案
推荐阅读
- google-calendar-api - 谷歌日历 API grantOfflineAccess() 没有弹出
- r - 无论列顺序如何,计算所有可能的组合
- validation - 时间字段中的 XPAGE validateexpression 问题
- matlab - 创建一个估计 e 并概述误差的 MATLAB 函数
- java - 使用 JVM 将堆保存到文件后,标量替换对象的重新分配失败
- javascript - 计算具有不同 id 和名称 attr 的动态创建的文本区域的字符
- xml - 未解决的参考:合成
- java - 如何填充动态列表 Java
- c# - 在 WPF 应用程序中使用 Net Driver 查询 Neo4j 并返回找到的节点
- sql - 使用 nvarchar(MAX) 的索引与基于具有固定字符的 nvarchar 的索引的性能?