[Tutor] SOS pleas help

Cameron Simpson cs at cskk.id.au
Mon Dec 6 18:32:14 EST 2021


On 06Dec2021 09:59, Deshaun Williams <deshwill at iu.edu> wrote:
>im trying to code a web application to use API but i cant get it to work

Doesn't work is a little vague. Some remarks on the code inline below:

>import math, json
>from bottle import request, route
>url = "https://www.gamerpower.com/api/giveaways?platform=pc"
>r = request.get(url)
>request.forms.get("cosnole")

This does a GET but does nothing with the result.

>gamer = request.forms.get("cosnole")

Both this GET and the preceeding one might misspell "console".

GETs typically want an entire URL.

>pathstring =""
>if gamer == "pc":
>    pathstring = ('/pc.html')

You don't need the brackets here, though they do no harm.

>elif gamer == "switch":
>    pathstring == ('/switch.html')

This is an equality test ("=="), not an assigment ("=").

>end

"end"? This is not a Python keyword. As such, it is a variable 
reference, and you have no "end" variable. This will produce a NameError 
when you get here.

>response =  request.get("https://www.gamerpower.com/api/giveaways?platform=pc")

Looks like url from earlier. If so, just say url here, it will avoid 
accidentally not using the same URL string.

>data = json.load(response)

json.load() is for files. You probably want json.loads() (load from a 
string). You should also check that response is actually a string, and 
not a more complex "HTTP response" type object. A:

    print(type(response))

before this line will tel you that.

>        # write the results in a csv file

I don't see anycode to write a CSV file. See the "csv" module 
documentation: https://docs.python.org/3/library/csv.html#module-csv

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list