【问题标题】:How can I access a URL using python requests when the page changes when I request it, even though the parameters are in the URL?当我请求页面更改时,如何使用 python 请求访问 URL,即使参数在 URL 中?
【发布时间】:2018-04-12 22:30:55
【问题描述】:

我正在尝试抓取以下网站: http://mlb.mlb.com/stats/sortable_batter_vs_pitcher.jsp#season=2018&batting_team=119&batter=571771&pitching_team=133&pitcher=641941

(这是一个带有特定投手/击球手匹配的示例 URL)

我可以通过这个功能轻松输入球员代码和球队代码:

def matchupURL(season, batter, batterTeam, pitcher, pitcherTeam):
return "http://mlb.mlb.com/stats/sortable_batter_vs_pitcher.jsp#season=" + str(season)+ "&batting_team="+str(teamNumDict[batterTeam])+"&batter="+str(batter)+"&pitching_team="+str(teamNumDict[pitcherTeam])+"&pitcher="+str(pitcher);

效果很好,并且返回的字符串在粘贴到我的浏览器时也可以使用。

但是当我提出请求时

newURL = matchupURL(2018,i.id,x.home_team,j.id,x.away_team)
        print(i+ " vs " + j)
        newSes = requests.get(newURL);
        html = BeautifulSoup(newSes.text, "lxml")
        mydivs = html.findAll("td",{"class":"dg-ops"})
        #do something with this div

我找不到 div。事实上,返回的 HTML 的整个格式都发生了变化。此外,添加标头没有帮助,使用 urllib 代替请求也没有帮助。

【问题讨论】:

  • 浏览器可能正在通过 JS 渲染 html 元素,使用 requests 之类的东西时不会运行。您应该考虑使用可以呈现页面的东西,例如浏览器。您是否考虑过无头 Chrome 或类似的东西?

标签: python web-scraping beautifulsoup python-requests


【解决方案1】:

这个页面是动态的,即内容是由javascript动态生成并显示在前面的。这就是您无法检测到div 标签的原因。

但在这种情况下,您可以更轻松地进行刮擦。使用浏览器中的检查工具,您可以检测到数据来自对 URL 的 GET 请求。对于您的示例,您只需提供玩家 ID:

import requests
url = 'http://lookup-service-prod.mlb.com/json/named.stats_batter_vs_pitcher_composed.bam'
params = {"sport_code":"'mlb'","game_type":"'R'","player_id":"571771","pitcher_id":"641941"}
resp = requests.get(url, params=params).json()
print(resp)

打印出来的:

{'stats_batter_vs_pitcher_composed': {'stats_batter_vs_pitcher_total': {'queryResults': {'created': '2018-04-12T22:21:47', 'totalSize': '1', 'row': {'hr': '1', 'gidp': '0', 'pitcher_first_last_html': 'Emilio Pagán', 'player': 'Hernandez, Enrique', 'np': '4', 'sac': '0', 'pitcher': 'Pagan, Emilio', 'rbi': '1', 'player_first_last_html': 'Enrique Hernández', 'tb': '4', 'bats': 'R', 'xbh': '1', 'bb': '0', 'slg': '4.000', 'avg': '1.000', 'pitcher_id': '641941', 'ops': '5.000', 'hbp': '0', 'pitcher_html': 'Pagán, Emilio', 'g': '', 'd': '0', 'so': '0', 'throws': 'R', 'sf': '0', 'tpa': '1', 'h': '1', 'cs': '0', 'obp': '1.000', 't': '0', 'ao': '0', 'r': '1', 'go_ao': '-.--', 'sb': '0', 'player_html': 'Hernández, Enrique', 'sbpct': '.---', 'player_id': '571771', 'ibb': '0', 'ab': '1', 'go': '0'}}}, 'copyRight': ' Copyright 2018 MLB Advanced Media, L.P.  Use of any content on this page acknowledges agreement to the terms posted here http://gdx.mlb.com/components/copyright.txt  ', 'stats_batter_vs_pitcher': {'queryResults': {'created': '2018-04-12T22:21:47', 'totalSize': '1', 'row': {'hr': '1', 'gidp': '0', 'pitcher_first_last_html': 'Emilio Pagán', 'player': 'Hernandez, Enrique', 'np': '4', 'sac': '0', 'pitcher': 'Pagan, Emilio', 'rbi': '1', 'opponent': 'Oakland Athletics', 'player_first_last_html': 'Enrique Hernández', 'tb': '4', 'xbh': '1', 'bats': 'R', 'bb': '0', 'avg': '1.000', 'slg': '4.000', 'pitcher_id': '641941', 'ops': '5.000', 'hbp': '0', 'pitcher_html': 'Pagán, Emilio', 'g': '', 'd': '0', 'so': '0', 'throws': 'R', 'sport': 'MLB', 'sf': '0', 'team': 'Los Angeles Dodgers', 'tpa': '1', 'league': 'NL', 'h': '1', 'cs': '0', 'obp': '1.000', 't': '0', 'ao': '0', 'season': '2018', 'r': '1', 'go_ao': '-.--', 'sb': '0', 'opponent_league': 'AL', 'player_html': 'Hernández, Enrique', 'sbpct': '.---', 'player_id': '571771', 'ibb': '0', 'ab': '1', 'opponent_id': '133', 'team_id': '119', 'go': '0', 'opponent_sport': 'MLB'}}}}}

【讨论】:

  • 非常感谢。我是一名学生,但我还没有上过网络课程,希望在那里我能更详细地学习如何解决此类问题。
  • 给未来观众的一个提示:我认为它应该是 .json() 而不仅仅是 .json 这对我有用,它是一种方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
  • 2023-03-24
  • 2014-01-17
  • 2012-03-30
  • 2018-09-03
  • 1970-01-01
相关资源
最近更新 更多