使用Python和Curl发布表单 [英] POSTing a form using Python and Curl

查看:142
本文介绍了使用Python和Curl发布表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个相对较新的(如在几天内)Python - 我正在寻找一个例子,将告诉我如何张贴表单到网站(如www.example.com)。

I am relatively new (as in a few days) to Python - I am looking for an example that would show me how to post a form to a website (say www.example.com).

我已经知道如何使用Curl。事实上,我写了C +++代码,完全一样的东西(即使用Curl POST表单),但我想一些起点(几行,我可以建立),这将告诉我如何做这个使用Python。

I already know how to use Curl. Infact, I have written C+++ code that does exactly the same thing (i.e. POST a form using Curl), but I would like some starting point (a few lines from which I can build on), which will show me how to do this using Python.

推荐答案

下面是一个使用urllib和urllib2的POST和GET示例:

Here is an example using urllib and urllib2 for both POST and GET:

POST - 如果 urlopen()有第二个参数,那么它是一个POST请求。

POST - If urlopen() has a second parameter then it is a POST request.

import urllib
import urllib2

url = 'http://www.example.com'
values = {'var' : 500}

data = urllib.urlencode(values)
response = urllib2.urlopen(url, data)
page = response.read()

GET - 如果 urlopen code>有一个参数,那么它是一个GET请求。

GET - If urlopen() has a single parameter then it is a GET request.

import urllib
import urllib2

url = 'http://www.example.com'
values = {'var' : 500}

data = urllib.urlencode(values)
fullurl = url + '?' + data
response = urllib2.urlopen(fullurl)
page = response.read()

如果你使用 os.system()调用它,你也可以使用 curl c $ c>。

You could also use curl if you call it using os.system().

以下是一些实用连结:

http://docs.python.org/library/urllib2.html#urllib2.urlopen

http://docs.python.org/library/os.html#os.system a>

Here are some helpful links:
http://docs.python.org/library/urllib2.html#urllib2.urlopen
http://docs.python.org/library/os.html#os.system

这篇关于使用Python和Curl发布表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆