【发布时间】:2018-05-09 09:10:09
【问题描述】:
这里是新手。我正在尝试使用 BeautifulSoup4 从网站上抓取一些体育统计数据。下面的脚本确实输出了一个表格,但它实际上并不是浏览器中出现的具体数据(浏览器中出现的数据是我所追求的数据——一个赛季的射手数据,而不是所有时间记录)。
#import libraries
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
#specify the url
stat_page = 'https://www.premierleague.com/stats/top/players/goals?se=79'
# query the website and return the html to the variable ‘page’
page = urlopen(stat_page)
#parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page, 'html.parser')
# Take out the <div> of name and get its value
stats = soup.find('tbody', attrs={'class': 'statsTableContainer'})
name = stats.text.strip()
print(name)
似乎在幕后进行了一些数据过滤,但我不确定如何使用 BeautifulSoup4 过滤输出。似乎在 HTML 之上发生了一些 Javascript 过滤。
我已尝试确定此特定过滤器是什么,并且似乎过滤已在此处完成。
<div class="current" data-dropdown-current="FOOTBALL_COMPSEASON" role="button" tabindex="0" aria-expanded="false" aria-labelledby="dd-FOOTBALL_COMPSEASON" data-listen-keypress="true" data-listen-click="true">2017/18</div>
我已阅读以下链接,但我不完全确定如何将其应用到我的答案中(再次,这里是初学者)。
Having problems understanding BeautifulSoup filtering
我尝试过安装、导入和应用不同的解析器,但总是遇到同样的错误(找不到树生成器)。关于如何从似乎使用 JS 过滤器的网站中提取数据的任何建议?
谢谢。
【问题讨论】:
标签: python web-scraping beautifulsoup