从列表中删除项目是不行的

时间:2017-01-03 16:50:30

标签: python list

我正在开展一个非常酷的项目,但我需要帮助。您可以看到我从sslproxies.org收集代理,但是将从表中收集的这些代理排序到没有额外信息的列表中非常困难。到目前为止,我的代码不起作用。希望你们可以提供帮助。我想做的是每两次删除列表中的第六项。

f = open("proxies.txt", 'w+')
def getProxy():
    url = "https://www.sslproxies.org"
    source_code = requests.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, "html.parser")
    global tlist
    tlist = []
    for tr in soup.find_all('tr'):
        for td in tr.find_all('td'):
            tlist.append(td)
    clist = tlist
    count = 0
    for word in clist:
        count += 1
        if count > 2:
            clist.remove(word)
            count += 1
            if count >= 6:
                count = 0
        else:
            continue
f.write(str(clist))

2 个答案:

答案 0 :(得分:1)

这是一个生成两个项目的生成器,然后跳过六个,然后再生成两个,等等

def skip_six(l):
    for i, x in enumerate(l):
        if i%8 <= 1:
            yield x

您可以使用它来制作像

这样的列表
clist = list(skip_six(tlist))

答案 1 :(得分:0)

我相信您要选择前2列。在这种情况下,您可能希望使用pandas read html尝试这样的操作。请注意,我无法访问您提到的网站。所以我还没有测试过这段代码

import pandas as pd
df=pd.read_html(io ='https://www.sslproxies.org')
print df
print df[['IP Address','Port']] # select the columns that you are interested in