Created
October 24, 2022 13:39
-
-
Save alejandrobernardis/64cc2d29b151a529f2b4eac5b94bd728 to your computer and use it in GitHub Desktop.
ENACOM Argentina Códigos de Área
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import re | |
import requests | |
import time | |
from bs4 import BeautifulSoup | |
url = 'https://www.enacom.gob.ar/areaslocales/busqueda/{pos}' | |
field = 'campo=' | |
pos = 1 | |
f_number = re.compile(r'\d+') | |
with open('output.csv', 'w', newline="") as f: | |
w = csv.writer(f, delimiter=';', quotechar='"', quoting=csv.QUOTE_ALL) | |
w.writerow(['id', 'city', 'state']) | |
while 1: | |
print(pos) | |
r = requests.post(url.format(pos=pos), data=field) | |
b = BeautifulSoup(r.text, 'html.parser') | |
for x in b.find_all('tr'): | |
if x.has_attr('class'): | |
break | |
row = [y.get_text(strip=True) for y in x.find_all('td')] | |
print(row) | |
if row: | |
w.writerow(row) | |
n = b.find('a', class_='next') | |
if not n: | |
break | |
f = f_number.search(n.get('onclick')) | |
if not f: | |
break | |
pos = f.group() | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment