Search code examples
pythonapipastebin

How can I take a few strings and put them in Pastebin, using the Pastebin API?


I'm working on a project right now, and it involves holding a lot of names, prices, etc. for a few different items. I want to be able to put all these items in a Pastebin paste.

I'm using Python 2.7, if it matters (I think urllib.urlopen is for Python 2.x and urllib.request.urlopen is for Python 3.x)

Here's my code right now:

#-*- encoding: UTF-8 -*-
import urllib

pastebin_vars = {'api_dev_key':'57fe1369d02477a235057557cbeabaa1','api_option':'paste','api_paste_code':'testing pastebin right now'}
response = urllib.urlopen('http://pastebin.com/api/api_post.php'[, urllib.parse.urlencode(pastebin_vars)[, read().encode('UTF-8')]])

Is there something wrong with the format or something? Here's the error I get when running the .py file:

AttributeError: 'module' object has no attribute 'parse'

I've tried playing around with the code. Since I got the AttributeError, I took out the part that said .parse and got this error:

NameError: Name 'read' is not defined

After that, I took out the part that said read(). and got this error:

NameError: Name 'encode' is not defined

Then I sighed, but removed the part that said ,encode('UTF-8') and had nothing return after executing the code.

So, what's up? What am I doing wrong? =/


Solution

  • Use urllib.urlencode()

    #-*- encoding: UTF-8 -*-
    import urllib
    
    pastebin_vars = {'api_dev_key':'57fe1369d02477a235057557cbeabaa1','api_option':'paste','api_paste_code':'testing pastebin right now'}
    response = urllib.urlopen('http://pastebin.com/api/api_post.php', urllib.urlencode(pastebin_vars))
    url = response.read()
    
    >>> print url
    http://pastebin.com/4wfzATQR