I am trying to parse some json from an API using python. The results are paginated into groups of 100, with a NextPageLink
entry in the json linking to the next page.
I have a class, with a parser that should call itself on the new response when hitting the NextPageLink
, but it doesnt seem to work.
Can someone explain why?
import requests
from requests.exceptions import HTTPError
class MyParser():
def __init__(self):
try:
self.response = requests.get("API end point url")
except HTTPError as http_err:
print("HTTP Error")
except:
print("Other Error")
def parse(self):
print("Called outer")
for item in self.response.json()["Items"]:
yield {
item["Thing"]:item["Entry"]
}
next_page = self.response.json()["NextPageLink"]
if next_page is not None:
self.response=requests.get(next_page)
print("about to call again")
self.parse()
print("Called")
Doesn't seem to work. I get from:
test = MyParser()
for i in test.parse():
print(i)
Output
Called outer
list of things yielded
about to call again
Called