Home > database >  Why json download with Python urllib.request is so slow? How can I make it faster?
Why json download with Python urllib.request is so slow? How can I make it faster?

Time:10-13

I perform the following code:

import urllib.request
from datetime import datetime

start = datetime.now()
user_agent = 'First Lastname [email protected]' # SEC uses this format as header.

url = 'https://data.sec.gov/submissions/CIK0000320193.json'
headers={'User-Agent':user_agent,} 

request=urllib.request.Request(url,None,headers) 
response = urllib.request.urlopen(request)
data = response.read()
print(datetime.now() - start)

It took me 0:02:33.646154 2.5 minutes. That doesn't make sense to me because I can open the link with browser in less than a second. Why does the code so slow? How could I make it faster? Thank you for any suggestions.

CodePudding user response:

I ran the exact code you provided and it returned in 0:00:00.515504 seconds.

Here are 3 reasons for why it might take you so long until it is returned.

  1. Your computer is incredibly slow.
  2. Your internet connection is slow/unstable.
  3. Maybe https://data.sec.gov deliberatly takes so long to return to you because you ran the code a lot of times and is giving you some kind of cool-down.
  • Related