【问题标题】:Getting json data from football site with python使用python从足球网站获取json数据
【发布时间】:2017-08-30 19:43:33
【问题描述】:

我正在尝试在此链接中抓取网站:

https://www.whoscored.com/Players/11119/Show/Lionel-Messi

我想从摘要、防御、进攻选项卡中获取数据,我尝试过 这个:

import requests
# This is the json data link i wish to get from the defensive tab (from developer tools)
url = "https://www.whoscored.com/StatisticsFeed/1/GetPlayerStatistics?category=summary&subcategory=defensive&statsAccumulationType=0&isCurrent=true&playerId=11119&teamIds=&matchId=&stageId=&tournamentOptions=&sortBy=Rating&sortAscending=&age=&ageComparisonType=&appearances=&appearancesComparisonType=&field=Overall&nationality=&positionOptions=&timeOfTheGameEnd=&timeOfTheGameStart=&isMinApp=false&page=&includeZeroValues=true&numberOfPlayersToPick="

response = requests.get(url)

print(response.json())

我无法打印 json 数据并从选项卡中抓取我需要的数据,我做错了什么吗? 感谢您的帮助。

【问题讨论】:

  • 该 URL 无效,至少如本问题所述,无效。
  • 响应是 HTML,而不是 JSON,所以这是行不通的。

标签: python json web-scraping python-requests scraper


【解决方案1】:

按照提供的方式执行代码时,响应返回 403“禁止”状态。

可能此提供者不希望他们的数据被抓取并检查请求是否存在非浏览器访问的迹象,并返回此错误。

在继续之前,请务必检查您的response.status_code

【讨论】:

  • 状态确实是403,有没有还能报废数据?
  • 您是否尝试过添加包含用户代理等基本内容的标题?
  • @jlaur 我确实尝试使用用户代理添加标头,我对请求库很陌生,不明白这些额外参数的作用,我一直在使用 requests.get(url ),没有添加任何参数。
  • 阅读:docs.python-requests.org/en/master/user/advanced。 Basicalle 你像这样实例化会话类:session = requests.Session()。然后,您首先查询网站的真实网址(您帖子中的第一个链接)。像这样:response = session.get(your_url, headers=your_header)。这应该在请求中存储一个 cookie。然后,您应该查询为您提供 403 的链接。real_response = session.get(json_url, headers=your_header)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 2022-06-11
  • 1970-01-01
  • 2021-08-04
  • 2020-08-05
  • 1970-01-01
  • 2021-07-26
相关资源
最近更新 更多