【发布时间】:2018-06-30 02:15:59
【问题描述】:
我正在尝试从balloon-reference.com 上抓取一些数据。我编写了一些代码来从站点的其他部分获取数据,其中表格的编码更简单一些,但特定的页面集显然更复杂。这是我到目前为止的代码。
从 urllib.request 导入 urlopen 从 bs4 导入 BeautifulSoup
# Declare URL
test_url = 'https://www.baseball-reference.com/boxes/SLN/SLN201704020.shtml'
# Query the website and return the HTML
page = urlopen(test_url)
# Parse the HTML and store
soup = BeautifulSoup(page, 'lxml')
table = soup.find("div", {"class": "table_outer_container"})
这并没有找到我想要的表格(在这个特定的页面上,两个表格包含 At-Bats、RBI、HRs、runs 等)。我尝试了其他一些方法,例如
table = soup.find_all("table" , {"class": "sortable stats_table"})
但它也不起作用。我也尝试使用 pandas 阅读该网站,但没有成功,所以如果有更简单的 pandas 方法,我也愿意。
【问题讨论】:
-
看起来页面的整个部分都以 HTML 编码作为注释。我以前见过这种情况(大概是为了绕过擦洗?)但不确定解决方法。
标签: python web-scraping beautifulsoup