Python 3.x,需要帮助逐行迭代代理文本文件

时间:2018-03-28 21:47:23

标签: python python-3.x

我对python相对较新,我正在尝试构建一个程序,该程序可以使用代理从文本文件中的代理列表访问网站,并继续对文件中的每个代理执行此操作,直到他们都被使用了。我在网上发现了一些代码,并根据我的需要进行了调整,但是当我运行程序时,代理程序已成功使用,但它们并没有按顺序使用。无论出于何种原因,第一个代理连续两次使用,然后第二个代理被使用,然后是第一个,然后是第三个,等等等等。它没有一个接一个地排序。

文本文件中的代理按此组织:

123.45.67.89:8080
987.65.43.21:8080

等等。这是我正在使用的代码:

from fake_useragent import UserAgent
import pyautogui
import webbrowser
import time
import random
import random
import requests
from selenium import webdriver
import os
import re

proxylisttext = 'proxylistlist.txt'
useragent = UserAgent()
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy_type", 1)

def Visiter(proxy1):
    try:
        proxy = proxy1.split(":")
        print ('Visit using proxy :',proxy1)
        profile.set_preference("network.proxy.http", proxy[0])
        profile.set_preference("network.proxy.http_port", int(proxy[1]))
        profile.set_preference("network.proxy.ssl", proxy[0])
        profile.set_preference("network.proxy.ssl_port", int(proxy[1]))
        profile.set_preference("general.useragent.override", useragent.random)
        driver = webdriver.Firefox(firefox_profile=profile)
        driver.get('https://www.iplocation.net/find-ip-address')
        time.sleep(2)
        driver.close()
    except:
        print('Proxy failed')
        pass

def loadproxy():
    try:
        get_file = open(proxylisttext, "r+")
        proxylist = get_file.readlines()
        writeused = get_file.write('used')
        count = 0
        proxy = []
        while count < 10:
            proxy.append(proxylist[count].strip())
            count += 1
            for i in proxy:
                Visiter(i)
    except IOError:
        print ("\n[-] Error: Check your proxylist path\n")
        sys.exit(1)

def main():
    loadproxy()
if __name__ == '__main__':
    main()

正如我所说的,这段代码使用代理成功导航到ipchecker站点,但是它没有按顺序逐行导航,相同的代理将被多次使用。所以我想更具体地说,我怎样才能确保程序逐个迭代代理,而不重复?我已经详尽地搜索了一个解决方案,但我还没能找到一个解决方案,所以任何帮助都会受到赞赏。谢谢。

2 个答案:

答案 0 :(得分:2)

你的问题在于这些嵌套循环,它们似乎没有做你想做的事情:

    proxy = []
    while count < 10:
        proxy.append(proxylist[count].strip())
        count += 1
        for i in proxy:
            Visiter(i)

外部循环构建proxy列表,每次添加一个值,直到有十个。在添加每个值之后,内部循环遍历到目前为止已构建的proxy列表,访问每个项目。

我怀疑你想要取消循环。这样,for循环只会在while循环完成后运行,因此它只会访问每个代理一次。尝试这样的事情:

    proxy = []
    while count < 10:
        proxy.append(proxylist[count].strip())
        count += 1
    for i in proxy:
        Visiter(i)

如果需要,您可以将其简化为单个循环。例如,使用itertools.islice来处理边界检查,您可以这样做:

for proxy in itertools.islice(proxylist, 10):
    Visiter(proxy.strip())

您甚至可以直接在文件对象上运行(因为文件是可迭代的),而不是先调用readlines,然后将其读入列表。 (在编写seek之前,您可能需要在文件上添加"used"调用,但无论如何,您可能需要这样做,某些操作系统不允许您混合读取和写入而不在其间寻找。)

答案 1 :(得分:1)

while count < 10: proxy.append(proxylist[count].strip()) count += 1 for i in proxy: Visiter(i)

while循环中的for循环意味着每次访问proxy.append时,您都会调用Visiter来获取已在代理中的每个项目。这可能解释了为什么每个代理都会获得多次点击。

就乱序问题而言,我不确定为什么readlines()不维护文件的行顺序,但我会尝试这样的事情:

with open('filepath', 'r') as file: for line in file: do_stuff_with_line(line) 有了上述内容,您不需要将整个文件同时保存在内存中,这对于大文件来说都很合适。

祝你好运!