How to optimize data scrape
I am scraping data from an external API to my database on Django. This method is taking a very long time to load. Is there a better way to do this? In addition, each time I reload the page, the data replicates inside the database. Here is the code from my views.py file:
def market_data(request):
# GET MARKETPLACE ASKS
NFT_list = {}
URL = '...&page_number=1'
response = requests.get(URL).json()
pages = response['result']['page_count']
page_index = 1
for page in range(pages):
URL = "...&page_number="+str(page_index)
response = requests.get(URL).json()
nfts = response['result']['ask_summaries']
for item in nfts:
if NFT.objects.filter(nft_data_id = ['nft_data_id']).exists():
pass
else:
nft_data = NFT(
nft_data_id = item['nft_data_id'],
brand = item['brand'],
price_low = item['price_low'],
price_high = item['price_high'],
nft_count = item['nft_count'],
name = item['name'],
series = item['series'],
rarity = item['rarity'],
)
nft_data.save()
NFT_list = NFT.objects.all()
page_index += 1
return render(request, 'main/market_data.html', {'NFT_list' : NFT_list} )