gpt4 book ai didi

python - 在for循环内尝试/Except行为异常

转载 作者:行者123 更新时间:2023-12-03 08:04:51 24 4
gpt4 key购买 nike

代码:

def ValidateProxy(LIST_PROXIES):
'''
Checks if scraped proxies allow HTTPS connection
'''

for proxy in LIST_PROXIES:

print('using', proxy)

host, port = str(proxy).split(":")

try:
resp = requests.get('https://amazon.com',
proxies=dict(https=f'socks5://{host}:{port}'),
timeout=6)

except ConnectionError:
print(proxy, 'REMOVED')
LIST_PROXIES.remove(proxy)


print(len(LIST_PROXIES), 'PROXIES GATHERED')

if len(LIST_PROXIES) != 0:
return LIST_PROXIES
else:
return None

输入:
['46.4.96.137:1080', '138.197.157.32:1080', '138.68.240.218:1080'.....] #15 proxies

输出:
using 46.4.96.137:1080
46.4.96.137:1080 REMOVED
using 138.68.240.218:1080
138.68.240.218:1080 REMOVED
using 207.154.231.213:1080
207.154.231.213:1080 REMOVED
using 198.199.120.102:1080
198.199.120.102:1080 REMOVED
using 88.198.24.108:1080
88.198.24.108:1080 REMOVED
using 188.226.141.211:1080
188.226.141.211:1080 REMOVED
using 92.222.180.156:1080
92.222.180.156:1080 REMOVED
using 183.233.183.70:1081
183.233.183.70:1081 REMOVED
7 PROXIES GATHERED # len(LIST_PROXIES) == 7, so 8 are removed which are printed above

我的疑问:
  • 为什么print('using', proxy)不会每次都执行? (因为输入列表有15个项目,该行仅被打印8次)
  • 是否每次都尝试执行,但两个块除外?因为每次REMOVED都打印在控制台上。
  • 我想为每个代理使用类似于print('using', proxy)的功能,如果ConnectionError然后是print(proxy, 'REMOVED')并从列表中删除该代理。

  • 编辑:全输入
    ['46.4.96.137:1080', '138.197.157.32:1080', '138.68.240.218:1080', '162.243.108.129:1080', '207.154.231.213:1080', '176.9.119.170:1080', '198.199.120.102:1080', '176.9.75.42:1080', '88.198.24.108:1080', '188.226.141.61:1080', '188.226.141.211:1080', '125.124.185.167:38801', '92.222.180.156:1080', '188.166.83.17:1080', '183.233.183.70:1081']

    最佳答案

    问题是您要遍历LIST_PROXIES并同时从中删除元素。

    如果您只想遍历LIST_PROXIES一次,则可以执行以下操作:

    def ValidateProxy(LIST_PROXIES):
    index = 0
    for i in range(len(LIST_PROXIES)):
    proxy = LIST_PROXIES[index]
    print('using', proxy)
    host, port = str(proxy).split(":")
    try:
    resp = requests.get('https://amazon.com',
    proxies=dict(https=f'socks5://{host}:{port}'),
    timeout=6)
    index += 1
    except ConnectionError:
    print(proxy, 'REMOVED')
    LIST_PROXIES.pop(index) # Index is not incremented
    print(len(LIST_PROXIES), 'PROXIES GATHERED')
    if len(LIST_PROXIES) != 0:
    return LIST_PROXIES
    else:
    return None

    但是,如果两次遍历列表都不成问题,您可以复制列表,如Sy Ker所指出的那样。

    关于python - 在for循环内尝试/Except行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62291417/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com