python - 所有行都期望第 0 行

标签 python html beautifulsoup html-parsing

from bs4 import BeautifulSoup
import urllib2
from lxml.html import fromstring
import re
import csv
import pandas as pd

wiki = "http://en.wikipedia.org/wiki/List_of_Test_cricket_records"
header = {'User-Agent': 'Mozilla/5.0'} #Needed to prevent 403 error on Wikipedia
req = urllib2.Request(wiki,headers=header)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)

try:
    table = soup.find_all('table')[1]
except AttributeError as e:
    print 'No tables found, exiting'

#gets all the tr tags

try:
    rows = table.find_all('tr')
except AttributeError as e:
    print 'No table rows found, exiting'

#gets only the 0th row        

try:
    first = table.find_all('tr')[0]
except AttributeError as e:
    print 'No table row found, exiting'

#how to get all rows expect the 0th one??
try:
    allRows = table.find_all('tr')
except AttributeError as e:
    print 'No table row found, exiting'
print allRows

我正在寻找一种方法来获取除第 0 行以外的所有行? 我知道如何获得第 0 行或任何特定行。但我希望每个“tr”标签/行都期望第 0 行。

任何建议

最佳答案

find_all()返回一个 ResultSet 实例,它是一个列表的子类,您可以 slice :

table.find_all('tr')[1:]

关于python - 所有行都期望第 0 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28598984/

相关文章:

python - 无法获取 dicom 图像以在 python 中显示

html - 每当我从导航菜单移动到下拉菜单时,悬停后​​下拉菜单就会消失

javascript - 如果多于 x 个字符则屏蔽

python - 使用 x 和 y 的数据框通过 Matplotlib 生成直方图

python - scipy interp1d 外推法 fill_value = tuple 不工作

python - 使用 Google AppEngine 验证码

javascript - 在 Mobile Safari 中禁用同源策略

python - 使用带有下拉选项的 Python 请求模块

python - 通过 Python 中的网页抓取工具登录网站

python - 修改带有换行符的 BeautifulSoup .string