如何使用requests.get()获取浏览器中的response的json数据呢?

如何使用requests.get()获取浏览器中的response的json数据呢?

如何使用requests.get()获取浏览器中的response的json数据呢?

  • 试着使用requests.get(r.text())打印出来的是网页返回数据不是response的json格式数据。
  • 试着使用print(json.dumps(req.text))也是不行。
讨论数量: 6
Jason990420

file

3周前 评论

使用req.json()得到如下:

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

这应该是json数据类型的问题

3周前 评论
Jason990420

Example for URL with json data

import requests

request_url = "https://data.nba.net/10s/prod/v3/today.json"
request_headers = {
    'authority': "data.nba.net",
    'method': "GET",
    'path': "/10s/prod/v3/today.json",
    'scheme': "https",
    'accept': "application/json, text/plain, */*",
    'accept-encoding': "gzip, deflate, br",
    'accept-language': "en,en-US",
    'origin': "https://www.nba.com",
    'sec-fetch-dest': "empty",
    'sec-fetch-mode': "cors",
    'sec-fetch-site': "cross-site",
    'user-agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
}
response = requests.get(request_url, headers=request_headers)
today_json = response.json()
for k, v in today_json['links'].items():
    print("{}: https://data.nba.net{}".format(k ,v))
3周前 评论

你试试result = requests.get().content.decode()然后json.loads(result)解析应该就可以了

3周前 评论
  • raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

应该是数据类型问题吧

3周前 评论
SilenceHL 3周前
Scrooge (作者) (楼主) 3周前
SandDio 3周前
import requests
import json

url = 'https://data.nba.net/10s/prod/v3/today.json'
print(json.loads(requests.get(url).content.decode()))

我这个代码就可以获取到数据了,数据为字典格式,如下: {'internal': {'pubDateTime': '2021-04-19 12:00:51.056 EDT', 'igorPath': 'cron,1618848049750,1618848049750|router,1618848049750,1618848049873|domUpdater,1618848049987,1618848050721|feedProducer,1618848050860,1618848052566', 'xslt': 'NBA/xsl/league/schedule/marty_today.xsl', 'xsltForceRecompile': 'true', 'xsltInCache': 'false', 'xsltCompileTimeMillis': '55', 'xsltTransformTimeMillis': '85', 'consolidatedDomKey': 'prod__transform__marty_today__1496522082129', 'endToEndTimeMillis': '2816'}, 'teamSitesOnly': {'seasonStage': 2, 'seasonYear': 2020, 'rosterYear': 2020, 'statsStage': 2, 'statsYear': 2020, 'displayYear': '2020-21', 'lastPlayByPlay': '/json/cms/noseason/game/{{gameDate}}/{{gameId}}/pbp_last.json', 'allPlayByPlay': '/data/10s/json/cms/noseason/game/{{gameDate}}/{{gameId}}/pbp_all.json', 'playerMatchup': '/data/10s/json/cms/2020/game/{{gameDate}}/{{gameId}}/playersPerGame.json', 'series': '/data/5s/json/cms/2020/regseason/team/{{teamUrlCode}}/series.json'}, 'seasonScheduleYear': 2020, 'showPlayoffsClinch': True, 'links': {'anchorDate': '20210419', 'currentDate': '20210419', 'calendar': '/prod/v1/multi/calendar.json', 'todayScoreboard': '/prod/v2/20210419/scoreboard.json', 'currentScoreboard': '/prod/v2/20210419/scoreboard.json', 'teams': '/prod/v2/2020/teams.json', 'scoreboard': '/prod/v2/{{gameDate}}/scoreboard.json', 'leagueRosterPlayers': '/prod/v1/2020/players.json', 'allstarRoster': '/prod/v1/allstar/2018/AS_roster.json', 'leagueRosterCoaches': '/prod/v1/2020/coaches.json', 'leagueSchedule': '/prod/v1/multi/schedule.json', 'leagueConfStandings': '/prod/v1/current/standings_conference.json', 'leagueDivStandings': '/prod/v1/current/standings_division.json', 'leagueUngroupedStandings': '/prod/v1/current/standings_all.json', 'leagueMiniStandings': '/prod/v1/current/standings_all_no_sort_keys.json', 'leagueTeamStatsLeaders': '/prod/v1/2020/team_stats_rankings.json', 'leagueLastFiveGameTeamStats': '/prod/v1/2020/team_stats_last_five_games.json', 'previewArticle': '/prod/v1/{{gameDate}}/{{gameId}}_preview_article.json', 'recapArticle': '/prod/v1/{{gameDate}}/{{gameId}}_recap_article.json', 'gameBookPdf': '/prod/v1/{{gameDate}}/{{gameId}}_Book.pdf', 'boxscore': '/prod/v1/{{gameDate}}/{{gameId}}_boxscore.json', 'miniBoxscore': '/prod/v1/{{gameDate}}/{{gameId}}_mini_boxscore.json', 'pbp': '/prod/v1/{{gameDate}}/{{gameId}}_pbp{{periodNum}}.json', 'leadTracker': '/prod/v1/{{gameDate}}/{{gameId}}lead_tracker{{periodNum}}.json', 'playerGameLog': '/prod/v1/2020/players/{{personId}}gamelog.json', 'playerProfile': '/prod/v1/2020/players/{{personId}}_profile.json', 'playerUberStats': '/prod/v1/2020/players/{{personId}}_uber_stats.json', 'teamSchedule': '/prod/v1/2020/teams/{{teamUrlCode}}/schedule.json', 'teamsConfig': '/prod/2020/teams_config.json', 'teamRoster': '/prod/v1/2020/teams/{{teamUrlCode}}/roster.json', 'teamsConfigYear': '/prod/{{seasonScheduleYear}}/teams_config.json', 'teamScheduleYear': '/prod/v1/{{seasonScheduleYear}}/teams/{{teamUrlCode}}/schedule.json', 'teamLeaders': '/prod/v1/2020/teams/{{teamUrlCode}}/leaders.json', 'teamScheduleYear2': '/prod/v1/{{seasonScheduleYear}}/teams/{{teamId}}/schedule.json', 'teamLeaders2': '/prod/v1/2020/teams/{{teamId}}/leaders.json', 'teamICS': '/prod/teams/schedules/2020/{{teamUrlCode}}_home_schedule.ics', 'teamICS2': '/prod/teams/schedules/2020/{{teamUrlCode}}_schedule.ics', 'playoffsBracket': '/prod/v1/2019/playoffsBracket.json', 'playoffSeriesLeaders': '/prod/v1/2019/playoffs{{seriesId}}_leaders.json', 'universalLinkMapping': 'www.nba.com/mobile/apps/configs/pr..., 'ticketLink': 'https://a.data.nba.com/tickets/single/{{seasonScheduleYear}}/{{gameId}}/{{trackingId}}'}}

3周前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!