zjm*_*126 29 python django urllib2
我不想使用html文件,但只有django我必须发出POST请求.
就像urllib2发送get请求一样.
gla*_*xly 32
urllib2和urllib的方法组合将起到作用.以下是我使用这两种方式发布数据的方法:
post_data = [('name','Gladys'),] # a sequence of two element tuples
result = urllib2.urlopen('http://example.com', urllib.urlencode(post_data))
content = result.read()
Run Code Online (Sandbox Code Playgroud)
urlopen()是一种用于打开网址的方法. urlencode()将参数转换为百分比编码的字符串.
Ric*_*ohr 30
以下是使用以下方法编写已接受答案示例的方法python-requests:
post_data = {'name': 'Gladys'}
response = requests.post('http://example.com', data=post_data)
content = response.content
Run Code Online (Sandbox Code Playgroud)
更直观.有关更简单的示例,请参阅快速入门.
| 归档时间: |
|
| 查看次数: |
51522 次 |
| 最近记录: |