【发布时间】:2018-06-21 10:14:10
【问题描述】:
背景:我正试图从这个pro-football-reference page. 中抓取一些表格我是 Python 的完全新手,所以很多技术术语最终都迷失在我身上,但在试图了解如何解决这个问题时,我想不通。
具体问题:因为页面上有多个表格,我不知道如何让 python 定位到我想要的那个。我正在尝试获取 Defense & Fumbles 表。下面的代码是我到目前为止所得到的,它是 from this tutorial 使用来自同一站点的页面 - 但只有一个表。
示例代码:
#url we are scraping
url = "https://www.pro-football-reference.com/teams/nwe/2017.htm"
#html from the given url
html=urlopen(url)
# make soup object of html
soup = BeautifulSoup(html)
# we see that soup is a beautifulsoup object
type(soup)
#
column_headers = [th.getText() for th in
soup.findAll('table', {"id": "defense").findAll('th')]
column_headers #our column headers
尝试:我意识到本教程的方法对我不起作用,因此我尝试更改 soup.findAll 部分以针对特定表。但我反复收到错误消息:
AttributeError:ResultSet 对象没有属性“findAll”。您可能将项目列表视为单个项目。当你打算调用 find() 时,你调用了 find_all() 吗?
改成find的时候,报错变成:
AttributeError: 'NoneType' 对象没有属性 'find'
老实说,我不知道自己在做什么,也不知道这些是什么意思。在确定如何定位该数据然后抓取它方面提供任何帮助,我将不胜感激。
谢谢,
【问题讨论】:
标签: python