【问题标题】:How to correctly set up input parameter of url in an HTTP GET request?如何在 HTTP GET 请求中正确设置 url 的输入参数?
【发布时间】:2018-11-02 19:03:10
【问题描述】:

编写一个名为“variable_get”的函数,该函数将字符串作为参数,表示 url 路径的一部分,并将 HTTPS GET 请求的响应作为字符串返回到 url“http://python.org/(input)”,其中 (input) 是该函数的输入参数。

import urllib2
def variable_get(input1):
    content = urllib2.urlopen('http://python.org/input1').read()
    return content

我的问题是如何在 HTTP GET 请求中正确设置 url 的输入参数?

【问题讨论】:

标签: python https


【解决方案1】:

在 Python > 3.6 中,您可以使用f-strings

from urllib import request
def variable_get(input1):
    url = f'http://python.org/{input1}'
    print(url)
    content = request.urlopen(url).read()
    return content

variable_get('somePath')

应该打印'http://python.org/somePath'

【讨论】:

  • 是的,在 Python3 中没有 urllib2。你需要使用urllib.request
  • 第 3 行的那个 'f' 字母会给你一个语法错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多