我正在努力弄清楚如何使用 Scrapy Python 抓取 JSON 响应。我能够成功地在同一站点的不同页面上抓取 JSON。我将不胜感激任何帮助。
我如何抓取“tournamentGroup”中的值(即id、名称)以及年份、标题等。
部分代码:
start_url = 'https://api.wtatennis.com/tennis/tournaments/?page=0&pageSize=100&excludeLevels=ITF&from=2020-09-01&to=2020-09-30'
with urllib.request.urlopen(start_url) as start_url:
json_obj = start_url.read()
rank_list = json.loads(json_obj)
for item in rank_list:
rank_data = []
tourney_id = item['content']['id']
tourney_year = item['year']
rank_data = [tourney_id, tourney_year]
cur.execute("""insert into wta_rankings(tourney_id, tourney_year)
values(%s, %s)
ON CONFLICT DO NOTHING"""
,(rank_data))
conn.commit()
cur.close()
JSON:
{
"pageInfo":{
"page":0,
"numPages":0,
"pageSize":100,
"numEntries":10
},
"content":[
{
"tournamentGroup":{
"id":2023,
"name":"Prague 125K",
"level":"125K",
"metadata":null
},
"year":2020,
"title":"Prague Open",
"startDate":"2020-08-29",
"endDate":"2020-09-06",
"surface":"Clay",
"inOutdoor":"O",
"city":"PRAGUE",
"country":"Czech Republic",
"singlesDrawSize":128,
"doublesDrawSize":32,
"prizeMoney":3125000,
"prizeMoneyCurrency":"USD",
"liveScoringId":"2023"
},
URL 示例:https://api.wtatennis.com/tennis/tournaments/?page =0&pageSize=100&excludeLevels=ITF&from=2020-09-01&to=2020-09-30
摇曳的蔷薇
相关分类