Last active
September 13, 2019 03:36
-
-
Save juanhuttemann/a93ded30b87c95bd6c887bbe2121500e to your computer and use it in GitHub Desktop.
Datos de Asegurados IPS (consulta en paralelo)
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
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"net/url" | |
"strconv" | |
"strings" | |
"sync" | |
"time" | |
"golang.org/x/net/html" | |
) | |
func scrap(ci string) bool { | |
var exists bool | |
response, err := http.PostForm( | |
"http://servicios.ips.gov.py/consulta_asegurado/comprobacion_de_derecho_externo.php", | |
url.Values{ | |
"nro_cic": {ci}, | |
"envio": {"ok"}, | |
"recuperar": {"Recuperar"}, | |
}, | |
) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer response.Body.Close() | |
z := html.NewTokenizer(response.Body) | |
content := []string{} | |
for z.Token().Data != "html" { | |
tt := z.Next() | |
if tt == html.StartTagToken { | |
t := z.Token() | |
if t.Data == "td" { | |
inner := z.Next() | |
if inner == html.TextToken { | |
text := (string)(z.Text()) | |
t := strings.TrimSpace(text) | |
content = append(content, t) | |
if len(content) > 4 { | |
exists = true | |
} | |
} | |
} | |
} | |
} | |
if len(content) > 4 { | |
fmt.Println(content[4:]) | |
} | |
return exists | |
} | |
func main() { | |
start := time.Now() | |
var contadorAsegurados int | |
var wg sync.WaitGroup | |
for i := 1999900; i < 2000000; i++ { | |
wg.Add(1) | |
ciString := strconv.Itoa(i) | |
go func(ciAsegurado string, contador int) { | |
var exists bool | |
defer wg.Done() | |
exists = scrap(ciAsegurado) | |
if exists { | |
contadorAsegurados++ | |
} | |
}(ciString, contadorAsegurados) | |
} | |
wg.Wait() | |
fmt.Println("Total recuperado", contadorAsegurados) | |
fmt.Println(time.Since(start)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment