【问题标题】:How can I take a few strings and put them in Pastebin, using the Pastebin API?如何使用 Pastebin API 获取一些字符串并将它们放入 Pastebin?
【发布时间】:2012-07-10 02:39:13
【问题描述】:

我现在正在做一个项目,它涉及为几个不同的项目保存很多名称、价格等。我希望能够将所有这些项目放入 Pastebin 粘贴中。

如果重要的话,我使用的是 Python 2.7(我认为 urllib.urlopen 用于 Python 2.x,urllib.request.urlopen 用于 Python 3.x)

这是我现在的代码:

#-*- 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')]])

格式有什么问题吗?这是我在运行 .py 文件时遇到的错误:

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

我尝试过使用代码。由于我得到了AttributeError,所以我把写着.parse的部分拿出来,得到了这个错误:

NameError: Name 'read' is not defined

之后,我把写着read().的部分拿出来,得到了这个错误:

NameError: Name 'encode' is not defined

然后我叹了口气,但是删除了说,encode('UTF-8')并且执行代码后没有任何返回的部分。

那么,怎么了?我究竟做错了什么? =/

【问题讨论】:

  • Errr - 看起来你已经从文档中复制粘贴了......从你发布的内容来看,我很惊讶解释器没有(更多)对你大喊大叫……

标签: python api pastebin


【解决方案1】:

使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 2012-07-07
    • 2011-11-26
    • 2022-07-29
    • 2012-10-14
    • 1970-01-01
    相关资源
    最近更新 更多