2017-07-03 31 views
0

我有這段代碼,我可以在Python 2.7的網站上搜索名爲「ctable」的所有表。但我想離開最後的1-2張桌子。 我該怎麼做?我如何才能離開最後一張網絡報紙表?

soup = BeautifulSoup(x, 'lxml') 
 

 
datatable=[] 
 
for ctable in soup.find_all('table', "ctable"): 
 

 
    for record in ctable.find_all('tr'): 
 
     temp_data = [] 
 
     for data in record.find_all('td'): 
 
      temp_data.append(data.text.encode('latin-1')) 
 
     datatable.append(temp_data) 
 

 
output.writerows(datatable)

回答

2

soup.find_all('table', "ctable")是一個列表(或迭代器),這樣你就可以離開k個最後元素與soup.find_all('table', "ctable")[:-k]

+0

謝謝你,這是工作循環! – tardos93

相關問題