Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
742 views
in Technique[技术] by (71.8m points)

python - Extract specific keys from multiple JSONs by multiple URLs through API's

I am using a specific API through a lottery service to extract results. To be more specific, I want to extract the winning numbers of the first draw of each day of the past month. The API url, I am using is the https://api.opap.gr/draws/v3.0/{gameId}/draw-date/{fromDate}/{toDate} which extracts only the draw IDs and provides the ability to put specific date ranges, but unfortunately I am getting the following error.

{"code":26,"message":"Validation Error: toDate:NotExceedMaxRange,fromDate:NotExceedMaxRange","data":[{"type":"NotExceedMaxRange","property":"toDate","value":"2020-12-02","inputType":"path"},{"type":"NotExceedMaxRange","property":"fromDate","value":"2020-12-01","inputType":"path"}],"source":"infostore","type":"ValidationException","guid":"cda6908c-362d-4368-918e-1c64456bf5e1"}

The error stops appearing when I place the same dates on the fields {fromDate}/{toDate}.

So I created the following code,

import requests
import json
import datetime

start = datetime.datetime(2020, 12, 1)
end = datetime.datetime(2020, 12, 26)
delta = datetime.timedelta(days=1)

fmt = "https://api.opap.gr/draws/v3.0/1100/draw-date/{date1:%Y-%m-%d}/{date1:%Y-%m-%d}/draw-id"

urls=[]
while start < end:
    date1 = start
    date2 = start + delta
    url = fmt.format(date1=date1, date2=date2)
    start = date2

print(url)
r=requests.get(url)
html=r.text
draws=json.loads(html)
print(draws)

I am getting the urls that I need to extract the JSON files from, but I am getting many errors basically because (at least I think) the data encased in each JSON is colossal.

Is there any method to extract the winning number key I want from each JSON???


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This will get you all the winning numbers on a given date (date is a string), won't let you query more than one day at a time:

import requests

def get_winning_numbers(date: str, game_id: str = '1100'):
    url = f'https://api.opap.gr/draws/v3.0/{game_id}/draw-date/{date}/{date}'
    data = requests.get(url).json()
    winning_numbers = [game['winningNumbers']['list']
                       for game in data['content']]
    return winning_numbers

print(get_winning_numbers('2020-12-01'))

Output:

[[49, 44, 55, 63, 79, 54, 19, 16, 59, 41, 4, 8, 23, 77, 36, 73, 35, 66, 3, 51],
 [3, 8, 24, 20, 4, 10, 33, 30, 65, 19, 68, 69, 17, 76, 39, 23, 27, 61, 49, 48],
 [66, 2, 50, 47, 24, 11, 25, 18, 80, 59, 22, 73, 48, 16, 65, 21, 5, 46, 67, 14],
 [32, 10, 8, 45, 69, 26, 72, 74, 13, 67, 79, 2, 25, 9, 58, 7, 20, 28, 66, 70],
 [46, 29, 41, 70, 72, 71, 34, 27, 38, 48, 68, 67, 31, 69, 74, 45, 11, 37, 75, 50],
 [28, 54, 69, 48, 79, 39, 60, 56, 9, 20, 11, 59, 4, 76, 61, 18, 78, 5, 64, 17],
 [7, 32, 31, 59, 68, 61, 20, 2, 15, 75, 23, 76, 74, 64, 37, 56, 41, 72, 65, 45],
 [23, 57, 4, 47, 51, 36, 16, 6, 17, 46, 29, 74, 35, 8, 15, 68, 33, 19, 40, 39],
 [14, 4, 23, 40, 36, 77, 31, 17, 5, 50, 35, 32, 39, 51, 38, 52, 26, 63, 30, 42],
 [25, 20, 66, 34, 22, 60, 43, 1, 40, 42, 31, 15, 4, 38, 26, 23, 75, 41, 44, 79]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...