【问题标题】:Can't Seem To Get POST Requests Working In Python 3似乎无法在 Python 3 中获取 POST 请求
【发布时间】:2012-05-21 12:44:58
【问题描述】:

我正在尝试编写一个脚本,允许我将图像上传到BayImg,但我似乎无法让它正常工作。据我所知,我没有得到任何结果。我不知道是不是提交数据还是什么,但是当我打印响应时,我得到的是主页的URL,而不是你上传图片时得到的页面。如果我使用 Python 2.x,我会使用 Mechanize。但是,它不适用于 Py3k,所以我尝试使用 urllib。我正在使用 Python 3.2.3。代码如下:

    #!/usr/bin/python3

    from urllib.parse import urlencode
    from urllib.request import Request, urlopen

    image = "/test.png"
    removal = "remove"
    tags = "python script test image"
    url = "http://bayimg.com/"
    values = {"code" : removal,
              "tags" : tags,
              "file" : image}

    data = urlencode(values).encode("utf-8")
    req = Request(url, data)
    response = urlopen(req)
    the_page = response.read()

任何帮助将不胜感激。

【问题讨论】:

    标签: python http post python-3.x urllib


    【解决方案1】:
    1. 你需要POST数据
    2. 你需要知道正确的url,查看html源码,本例为:http://upload.bayimg.com/upload
    3. 您需要读取文件的内容,而不是只传递文件名

    您可能希望使用Requests 轻松完成。

    【讨论】:

      【解决方案2】:

      我遇到了这篇文章,并想用下面的解决方案来改进它。所以这是一个用 Python3 编写的示例类,它使用 urllib 实现了 POST 方法。

      import urllib.request
      import json
      
      from urllib.parse import urljoin
      from urllib.error import URLError
      from urllib.error import HTTPError
      
      class SampleLogin():
      
          def __init__(self, environment, username, password):
              self.environment = environment
              # Sample environment value can be: http://example.com
              self.username = username
              self.password = password
      
          def login(self):
              sessionUrl = urljoin(self.environment,'/path/to/resource/you/post/to')
              reqBody = {'username' : self.username, 'password' : self.password}
              # If you need encoding into JSON, as per http://stackoverflow.com/questions/25491541/python3-json-post-request-without-requests-library
              data = json.dumps(reqBody).encode('utf-8')
      
              headers = {}
              # Input all the needed headers below
              headers['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"
              headers['Accept'] = "application/json"
              headers['Content-type'] = "application/json"
      
              req = urllib.request.Request(sessionUrl, data, headers)
      
              try: 
                  response = urllib.request.urlopen(req)
                  return response
              # Then handle exceptions as you like.
              except HTTPError as httperror:
                  return httperror
              except URLError as urlerror:
                  return urlerror
              except:
                  logging.error('Login Error')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-22
        • 1970-01-01
        • 1970-01-01
        • 2015-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多