Python переключает переменные во 2-м цикле?

#python #python-3.x

#python #python-3.x

Вопрос:

 proxies1 = {'http': 'http://199.193.251.1:3128', 'https': 'http://199.193.251.1:3128'}
proxies2 = {'http': 'http://199.193.251.1:3128', 'https': 'http://199.193.251.1:3128'}
proxies3 = {'http': 'http://199.193.251.1:3128', 'https': 'http://199.193.251.1:3128'}
proxies4 = {'http': 'http://199.193.251.1:3128', 'https': 'http://199.193.251.1:3128'}
proxies5 = {'http': 'http://199.193.251.1:3128', 'https': 'http://199.193.251.1:3128'}
proxies6 = {'http': 'http://199.193.251.1:3128', 'https': 'http://199.193.251.1:3128'}
proxies7 = {'http': 'http://199.193.251.1:3128', 'https': 'http://199.193.251.1:3128'}
proxies8 = {'http': 'http://199.193.251.1:3128', 'https': 'http://199.193.251.1:3128'}
proxies9 = {'http': 'http://199.193.251.1:3128', 'https': 'http://199.193.251.1:3128'}
proxies10 = {'http': 'http://199.193.251.1:3128', 'https': 'http://199.193.251.1:3128'}


ProxyList = [proxies1,proxies2,proxies3,proxies4,proxies5,proxies6,proxies7,proxies8,proxies9,proxies10]
  

Я пытаюсь переключать прокси каждый раз, когда этот цикл повторяется 2 раза…

 for channel in ChannelList:
    ChannelURL = ("https://url.com/b/"   str(channel)   "/app/basic/a/plusone/buzz:"   videoID   "?cbp=ck8a3bhdyjckamp;sview=1amp;cid=5amp;soc-app=115amp;soc-platform=1amp;spath=/b/"   str(channel)  "/app/basic/stream/"   videoID)
    soup = BeautifulSoup(s.get(ChannelURL, PROXY VARIABLE GOES HERE).text, "html.parser")
    for inp in soup.select(".jlvUSc input[name]"):
        if inp["name"] not in form_data1:
            form_data1[inp["name"]] = inp["value"]
    s.post(ChannelURL, form_data1)
  

Python 3.4

Я работаю с запросом Python

Ответ №1:

Это будет переключать прокси каждые два цикла:

 for loop,channel in enumerate(ChannelList):
    proxies = ProxyList[loop // 2 % len(ProxyList)]
  

Комментарии:

1. просто чтобы быть создателем, я заменяю for channel in ChannelList: на for loop,channel in enumerate(ChannelList): ?

2. Опечатка ^ Я имел в виду, что

Ответ №2:

Итак, что вам нужно, так это в каждом цикле инвертировать флаг. Когда ваш флаг будет равен true, получите следующий прокси из списка

 proxyflag = False
Indexer =0
for channel in ChannelList:
if (proxyflag):
  Indexer =1
ProxyIndex = Indexer%len(ProxyList)
Proxy = ProxyList[ProxyIndex]
ChannelURL = ("https://url.com/b/"   str(channel)   "/app/basic/a/plusone/buzz:"   videoID   "?cbp=ck8a3bhdyjckamp;sview=1amp;cid=5amp;soc-app=115amp;soc-platform=1amp;spath=/b/"   str(channel)  "/app/basic/stream/"   videoID)
soup = BeautifulSoup(s.get(ChannelURL, Proxy).text, "html.parser")
for inp in soup.select(".jlvUSc input[name]"):
    if inp["name"] not in form_data1:
        form_data1[inp["name"]] = inp["value"]
s.post(ChannelURL, form_data1)
proxyflag = not(proxyflag)