【问题标题】:BeautifulSoup4 output with JS Filters带有 JS 过滤器的 BeautifulSoup4 输出
【发布时间】: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


    【解决方案1】:

    在这些情况下,使用浏览器的开发工具跟踪网络请求通常很有用,因为通常使用 AJAX 检索数据,然后使用 JS 在浏览器中显示。

    在这种情况下,您要查找的数据似乎可以在以下位置访问:

    https://footballapi.pulselive.com/football/stats/ranked/players/goals?page=0&pageSize=20&compSeasons=79&comps=1&compCodeForActivePlayer=EN_PR&altIds=true
    

    它具有标准 JSON 格式,因此您应该能够轻松解析和提取数据。

    但是,请注意,此端点需要将 Origin HTTP 标头设置为 https://www.premierleague.com 才能满足您的请求。

    【讨论】:

      猜你喜欢
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 2017-01-12
      • 1970-01-01
      • 2014-09-08
      • 2012-12-14
      • 2019-05-16
      相关资源
      最近更新 更多