简体   繁体   中英

How I can leave the last web-scraping table?

I have got this code, which one i can search the all table called "ctable" on a website with Python 2.7. But i want to leave the last 1-2 table. How i can do this?

 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)

soup.find_all('table', "ctable")是一个列表(或一个迭代器),因此您可以将最后的 k 个元素与soup.find_all('table', "ctable")[:-k]一起循环

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM