POSTing form using Python and Curl

I'm relatively new (like in a few days) for Python - I'm looking for an example that will show me how to submit a form to a website (say www.example.com).

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

0
source share
3 answers

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

POST If urlopen() has a second parameter, this 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 If urlopen() has one parameter, then this 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() 

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

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

+2
source
 curl -d "birthyear=1990&press=AUD" www.site.com/register/user.php 

http://curl.haxx.se/docs/httpscripting.html

0
source

There are two main Python packages for automating web interactions:

  • Mechanize
  • Twill

    Twill apparently has not been updated for a couple of years and seems to have been with version 0.9 since December 2007. The mechanism shows changes and releases just a few days ago: 2010-05-16 with the release of version 0.2. one.

    Of course, you will find examples listed on their respective web pages. Twill essentially provides a simple interpreter, such as an interpreter, while Mechanize provides a class and API in which you, for example, set form values ​​using Python dictionary __setattr__() ( __setattr__() method). Both use BeautifulSoup to parse real-world HTML code (sloppy tag soup). (This is highly recommended for working with HTML that you encounter in the wild, and is strongly discouraged for your own HTML code, which must be written to pass standards that confirm, validate, parsers)

0
source

Source: https://habr.com/ru/post/1497482/


All Articles