【发布时间】:2019-03-25 10:01:00
【问题描述】:
我正在尝试从this fantasy basketball page 中抓取一些东西。我在 Python 3.5+ 中使用 BeautifulSoup 来做到这一点。
source_code = requests.get('http://fantasy.espn.com/basketball/league/standings?leagueId=633975')
plain_text = source_code.text
soup = BeautifulSoup(plain_text, 'lxml')
首先,我想将 9 个类别的标题抓取到 Python 列表中。所以我的列表应该看起来像categories = [FG%, FT%, 3PM, REB, AST, STL, BLK, TO, PTS]。
我希望做的事情如下:
tableSubHead = soup.find_all('tr', class_='Table2__header-row')
tableSubHead = tableSubHead[0]
listCats = tableSubHead.find_all('th')
categories = []
for cat in listCats:
if 'title' in cat.attrs:
categories.append(cat.string)
但是,soup.find_all('tr', class_='Table2__header-row') 返回一个空列表,而不是我想要的表格行元素。我怀疑这是因为当我查看页面源代码时,它与 Chrome Dev Tools 中的 Inspect Element 完全不同。我知道这是因为 Javascript 会动态更改页面上的元素,但我不确定解决方案是什么。
【问题讨论】:
标签: python web-scraping beautifulsoup