This file looks large and may slow your browser down if we attempt
to syntax highlight it, so we are showing it without any
pretty colors.
Highlight
it anyway.
| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
|
| 5 |
import csv |
| 6 |
import urllib |
| 7 |
import urllib2 |
| 8 |
|
| 9 |
#url = 'http://www1.previdencia.gov.br/pg_secundarias/' \ |
| 10 |
# 'paginas_perfis/perfil_comPrevidencia_09_04-A.asp' |
| 11 |
|
| 12 |
URL = 'http://www.previdencia.gov.br/devedores/consdeved.asp' |
| 13 |
|
| 14 |
# fake user-agent |
| 15 |
HEADERS = {'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686; en-US; ' \ |
| 16 |
'rv:1.9.0.2) Gecko/2008092313 Ubuntu/8.04 (hardy) ' \ |
| 17 |
'Firefox/3.1.6'} |
| 18 |
|
| 19 |
content = csv.reader(open('cpfsenadores.csv'), delimiter=';') |
| 20 |
for row in content: |
| 21 |
if len(row) < 2: |
| 22 |
continue |
| 23 |
|
| 24 |
name = row[0] |
| 25 |
cpf = row[1] |
| 26 |
|
| 27 |
print name, cpf |
| 28 |
|
| 29 |
try: |
| 30 |
int(cpf) |
| 31 |
except ValueError: |
| 32 |
continue |
| 33 |
|
| 34 |
body = { |
| 35 |
'checkcpf': 'on', |
| 36 |
'cpf': cpf} |
| 37 |
|
| 38 |
request = urllib2.Request(url=URL) |
| 39 |
|
| 40 |
for key in HEADERS: |
| 41 |
request.add_header(key, HEADERS[key]) |
| 42 |
|
| 43 |
body = urllib.urlencode(body) |
| 44 |
request.add_data(body) |
| 45 |
|
| 46 |
response = urllib2.urlopen(request) |
| 47 |
data = response.read() |
| 48 |
response.close() |
| 49 |
|
| 50 |
file('result.txt', 'w').write(data) |
| 51 |
break |