2017-01-03 39 views
0

我正在開發一個非常酷的項目,但我需要幫助。您可以看到im從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)) 
+4

如果您正在向前迭代,則不會從列表中刪除項目。見:http://stackoverflow.com/documentation/python/3553/common-pitfalls/12259/list-multiplication-and-common-references#t=201701031652321895116 – MooingRawr

+0

也,你的'如果計數> = 6你確定這是好的? –

+0

你能更清楚一點你想要什麼嗎?你想保留兩個項目,然後刪除四個,然後保留兩個,然後刪除四個,等等? –

回答

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 
1

這裏是一個發生器,產生兩個項目,然後跳過六,然後產生兩個以上等

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

您可以使用它來做出這樣一個列表

clist = list(skip_six(tlist)) 
+0

這是完美的。先生,你真是太棒了。謝謝。 –