从表中刮掉一列

时间:2014-04-08 18:59:39

标签: python beautifulsoup mechanize

我正在尝试从表格website

中删除表格中的最后三列

我能够首先制作一个脚本来显示所有列但是现在我想要最后三个我在努力使用下面的代码但是我遇到了错误。

#!/usr/bin/env python
from mechanize import Browser
from bs4 import BeautifulSoup
import csv

mech = Browser()
url = "http://en.wikipedia.org/wiki/Hybrid_electric_vehicles_in_the_United_States"
soup = BeautifulSoup(mech.open(url))
table = soup.find("table", class_="wikitable")

for row in table('tr')[1:]: print "|".join(col.text.strip() for col in row.find_all('th'))
for row in table('tr')[1:]: print "|".join(col.text.strip() for col in row.find_all('td') if col in row.findAll('th')[0].find('All hybrids sold').text)

我收到此错误

Traceback (most recent call last):
  File "scrap1.py", line 12, in <module>
    for row in table('tr')[1:]: print "|".join(col.text.strip() for col in row.f
ind_all('td') if col in row.findAll('th')[0].find('All hybrids sold').text)
  File "scrap1.py", line 12, in <genexpr>
    for row in table('tr')[1:]: print "|".join(col.text.strip() for col in row.f
ind_all('td') if col in row.findAll('th')[0].find('All hybrids sold').text)
IndexError: list index out of range

0 个答案:

没有答案