迭代两个大小不同的列表

时间:2018-07-17 09:15:23

标签: python-3.x selenium-webdriver web-scraping proxy

import itertools
Value = ['178.217.107.8:53281', '91.90.191.238:8080', '27.116.51.114:8080']
content = ['link1','link2','link3']

for a,b in zip(Value , itertools.cycle(content)):
   print(a,b)

我要寻找的是,如果第一个列表(代理列表)中的任何一个代理不起作用,则将其传递并移至列表中的下一个,并与内容列表中的元素平行当前阶段应该保持不变。

例如: 对于第一个元素,输出为: 178.217.107.8:53281 Link1

但是,如果178.217.107.8:53281抛出错误,则在循环中取值'a'为 并输出为91.90.191.238:8080 Link1

2 个答案:

答案 0 :(得分:1)

下面的伪代码:

list_of_proxies = ['178.217.107.8:53281', '91.90.191.238:8080', '27.116.51.114:8080']
list_of_links = ['link1','link2','link3']
for proxy in list_of_proxies:
    for link in list_of_links:
        # call some method to check if the proxy worked and store in a variable
        success = method_call(proxy, link) # assuming return type as boolean
        if !success:
            break
        else:
            # remove the successful link from the list, depending on your exact requirement
            list_of_links.remove(link)

答案 1 :(得分:0)

在这种情况下,您需要遍历链接,对于每个链接,请使用itertools.cycle在代理列表中循环,直到成功获取链接为止。

伪代码:

from itertools import cycle
Value = ['178.217.107.8:53281', '91.90.191.238:8080', '27.116.51.114:8080']
content = ['link1','link2','link3']
proxies = cycle(Value)

for link in content:
    while True:
        response = get_link_via_proxy(link, next(proxies))
        if response.is_success:
           break
    do_something_with(response)