简体   繁体   中英

Having trouble trying to pull data from a url into python using an API

I'm trying to pull data from a chart on a site in csv format. I've tried different combinations of code but can't seem to figure it out. I keep getting the following errors depending on the code I write:

TypeError: 'set' object is not subscriptable TypeError: a float is required

My latest attempt looked like this:

import urllib   
import urllib2    
import csv    
import StringIO


url = "https://api.rjmetrics.com/0.1/chart/chartid/export"    
headers = {"X-RJM-API-Key": "myapikey"}
data = {"format=csv"}
response = urllib2.Request(url, data, headers)    
re = urllib2.urlopen(response)    
spamreader = csv.reader(re, delimiter=',', quotechar='|')    
for row in spamreader:    
    print row

The working CURL version looks like this:

curl -d "format=csv" -H "X-RJM-API-Key: myapikey" https://api.rjmetrics.com/0.1/chart/chartid/export

but I don't know how to work with curl.

Thank you!

For this kind of task you should use the Requests package. That way you can properly set your header:

headers = {'X-RJM-API-Key': 'myapikey', 'content-type: text/csv'}
r = requests.get('https://api.rjmetrics.com/0.1/chart/chartid/export', headers=headers)

and then use the default csv reader.

data should be a url encoded string, you are passing it a set -- {"format=csv"} is a set literal. Try this:

data = urllib.urlencode(dict(format='csv'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM