Search code examples
pythonhttpsurllib2

https get request with python urllib2


I am trying to fetch data from quandl using urllib2.Please check code below.

import json
from pymongo import MongoClient
import urllib2
import requests
import ssl
#import quandl
codes = [100526];
for id in codes:
    url = 'https://www.quandl.com.com//api/v3/datasets/AMFI/"+str(id)+".json?api_key=XXXXXXXX&start_date=2013-08-30'
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    data = response.read()
    print data

OR

for id in codes:
    url = "https://www.quandl.com.com//api/v3/datasets/AMFI/"+str(id)+".json?api_key=XXXXXXXX&start_date=2013-08-30"
    request = requests.get(url,verify=False)
    print request

I am getting HTTPERROR exception 404 in 1st case. and when I use request module I get SSL error even after using verify=false. I am looking through previous posts but most of them are related to HTTP request.

Thanks for help. J


Solution

  • You seem to be using a wrong URL (.com.com instead of .com) as well as a combination of different quotes in the first version of your code. Use the following instead and it should work:

    import urllib2
    import requests
    
    codes = [100526]
    
    for id in codes:
        url = "https://www.quandl.com//api/v3/datasets/AMFI/"+str(id)+".json?start_date=2013-08-30"
        req = urllib2.Request(url)
        response = urllib2.urlopen(req)
        print response.read()
    
    for id in codes:
        url = "https://www.quandl.com//api/v3/datasets/AMFI/"+str(id)+".json?start_date=2013-08-30"
        response = requests.get(url,verify=False)
        print response.text
    

    To disable the warning about the SSL certificate, use the following code before making the request using requests:

    from requests.packages.urllib3.exceptions import InsecureRequestWarning
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)