How to make a POST request in Python?

Asked

Viewed 3,700 times

6

I’d like to know how to make a request POST in this URL, and then arrive on that page.

Where I have to spend the year at div of the tax body without signature.

  • You want to write a robot that manipulates this page, that’s it?

  • 1

    Yeah, I’m making a robot that downloads her pdfs.

  • You’ve already got some code started?

1 answer

6

There are several ways to do this what you want, the most common are with the use of module functions urllib, httplib (in Python 3 is http.client) or requests.

To page that you want to make the request, apparently you receive two parameters, ano and entrar. A way to do this with the module urllib is:

def funcao(url, ano):
    parametros = urllib.urlencode({'ano': ano, 'entrar': 'Buscar'})
    html = urllib.urlopen(url, parametros).read()
    return html

To get the result do the following:

url  = "http://venus.maringa.pr.gov.br/arquivos/orgao_oficial/paginar_orgao_oficial_ano.php"
ano  = 2015
html = funcao(url, ano)
print (html)

To extract the information from this HTML you can use regular expressions or an parser (parser), for the latter, the module can be used Beautiful Soup, example:

soup = BeautifulSoup(html)
# Extrai todos os links do elemento a
for link in soup.findAll('a'):
    print(link.get('href'))

Full example:

import urllib
from BeautifulSoup import BeautifulSoup # Ou: from bs4 import BeautifulSoup

def funcao(url, ano):
    parametros = urllib.urlencode({'ano': ano, 'entrar': 'Buscar'})
    html = urllib.urlopen(url, parametros).read()
    return html

url  = "http://venus.maringa.pr.gov.br/arquivos/orgao_oficial/paginar_orgao_oficial_ano.php"
ano  = 2015
html = funcao(url, ano)
soup = BeautifulSoup(html)

for link in soup.findAll('a'):
    print(link.get('href'))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.