Python在每个第二个循环中切换变量?

2024-04-24 18:58:44 发布

您现在位置:Python中文网/ 问答频道 /正文

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]

我试着每次循环两次就切换代理。。。你知道吗

for channel in ChannelList:
    ChannelURL = ("https://url.com/b/" + str(channel) + "/app/basic/a/plusone/buzz:" + videoID + "?cbp=ck8a3bhdyjck&sview=1&cid=5&soc-app=115&soc-platform=1&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 Request一起工作


Tags: nameinhttpsformapphttpchannelinp
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=ck8a3bhdyjck&sview=1&cid=5&soc-app=115&soc-platform=1&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)

这将每两个循环切换一次代理:

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

相关问题 更多 >