如何使用Python BeautifulSoup从网页解析选定的表

时间:2017-06-07 19:24:13

标签: python-3.x parsing beautifulsoup html-table

我希望从本地体育赛事中解析结果表(页面基本上只包含一个表格),但是当我尝试使用下面的脚本时,我只得到“菜单”,而不是实际的结果列表。我错过了什么?

from urllib.request import urlopen
from bs4 import BeautifulSoup
import pandas as pd

site = "https://rittresultater.no/nb/sb_tid/923?pv2=11027&pv1=U"
html = urlopen(site)
soup = BeautifulSoup(html, "lxml") #BeautifulSoup(urlopen(html, "lxml"))

table = soup.select("table")
df = pd.read_html(str(table))[0]
print.df

1 个答案:

答案 0 :(得分:1)

这是因为该页面上有两个<table>。您可以使用.table-condensed函数的class_参数查询所需表的类名(在本例中为find()),或者您可以在第二个表中查询使用find_all()函数的所有表的列表。

解决方案1:

table = soup.find('table', class_='table-condensed')
print(table)

解决方案2:

tables = soup.find_all('table')
print(tables[1])