Advertisement
antikorps

Untitled

Apr 13th, 2022
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.65 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "fmt"
  6.     "io/ioutil"
  7.     "log"
  8.     "net/http"
  9.     "regexp"
  10.     "strings"
  11.     "sync"
  12. )
  13.  
  14. type Resultado struct {
  15.     Titulo      string
  16.     Descripcion string
  17.     URL         string
  18.     Precio      string
  19. }
  20.  
  21. type PaginaMA struct {
  22.     Error      error
  23.     Endpoint   string
  24.     Resultados []Resultado
  25. }
  26.  
  27. type RespuestaMilAnunciosMin struct {
  28.     AdList struct {
  29.         Ads []struct {
  30.             Description string `json:"description"`
  31.             Price       struct {
  32.                 CashPrice struct {
  33.                     Text string `json:"text"`
  34.                 } `json:"cashPrice"`
  35.             } `json:"price"`
  36.             Title string `json:"title"`
  37.             URL   string `json:"url"`
  38.         } `json:"ads"`
  39.     } `json:"adList"`
  40. }
  41.  
  42. var wg sync.WaitGroup
  43.  
  44. func scrap_ma(cliente *http.Client, endpointMA *string, canal chan PaginaMA) chan PaginaMA {
  45.     defer wg.Done()
  46.  
  47.     var resultados []Resultado
  48.  
  49.     peticionMA, errorPeticionMA := http.NewRequest("GET", *endpointMA, nil)
  50.     if errorPeticionMA != nil {
  51.         canal <- PaginaMA{
  52.             Error:    errorPeticionMA,
  53.             Endpoint: *endpointMA,
  54.         }
  55.         return canal
  56.     }
  57.  
  58.     respuestaMA, errorRespuestaMA := cliente.Do(peticionMA)
  59.     if errorRespuestaMA != nil {
  60.         canal <- PaginaMA{
  61.             Error:    errorPeticionMA,
  62.             Endpoint: *endpointMA,
  63.         }
  64.         return canal
  65.     }
  66.     defer respuestaMA.Body.Close()
  67.  
  68.     contenidoPagina, errorContenidoPagina := ioutil.ReadAll(respuestaMA.Body)
  69.     if errorContenidoPagina != nil {
  70.         canal <- PaginaMA{
  71.             Error:    errorContenidoPagina,
  72.             Endpoint: *endpointMA,
  73.         }
  74.         return canal
  75.     }
  76.  
  77.     html := string(contenidoPagina)
  78.  
  79.     expRegMin := regexp.MustCompile(`\n`)
  80.     html = expRegMin.ReplaceAllString(html, "")
  81.     expRegAds := regexp.MustCompile(`.*?<script>window\.__INITIAL_.*?adListPagination...`)
  82.     html = expRegAds.ReplaceAllString(html, "")
  83.     expRegPag := regexp.MustCompile(`.."pagination.*`)
  84.     html = expRegPag.ReplaceAllString(html, "")
  85.  
  86.     expRegularEscapeComillas := regexp.MustCompile(`\\\\\\"`)
  87.     html = expRegularEscapeComillas.ReplaceAllString(html, `#_#_#_#_#"`)
  88.  
  89.     expRegularComillas := regexp.MustCompile(`\\"`)
  90.     html = expRegularComillas.ReplaceAllString(html, `"`)
  91.  
  92.     caracteresEscape := regexp.MustCompile(`#_#_#_#_#"`)
  93.     html = caracteresEscape.ReplaceAllString(html, `\"`)
  94.  
  95.     html = html + "}"
  96.     var respuestaMilAnunciosMin RespuestaMilAnunciosMin
  97.     errorConverJSON := json.Unmarshal([]byte(html), &respuestaMilAnunciosMin)
  98.  
  99.     if errorConverJSON != nil {
  100.         canal <- PaginaMA{
  101.             Error:    errorConverJSON,
  102.             Endpoint: *endpointMA,
  103.         }
  104.         return canal
  105.     }
  106.  
  107.     for _, v := range respuestaMilAnunciosMin.AdList.Ads {
  108.         var resultado Resultado
  109.         resultado.Titulo = v.Title
  110.         resultado.Descripcion = v.Description
  111.         resultado.Precio = v.Price.CashPrice.Text
  112.         resultado.URL = "https://www.milanuncios.com" + v.URL
  113.         resultados = append(resultados, resultado)
  114.     }
  115.  
  116.     canal <- PaginaMA{
  117.         Error:      nil,
  118.         Endpoint:   *endpointMA,
  119.         Resultados: resultados,
  120.     }
  121.     return canal
  122. }
  123.  
  124. func main() {
  125.  
  126.     cliente := http.Client{}
  127.     canal := make(chan PaginaMA)
  128.     for i := 1; i < 11; i++ {
  129.         endpointMA := fmt.Sprint("https://www.milanuncios.com/portatiles-de-segunda-mano/?pagina=", i)
  130.         wg.Add(1)
  131.         go scrap_ma(&cliente, &endpointMA, canal)
  132.     }
  133.  
  134.     go func() {
  135.         wg.Wait()
  136.         close(canal)
  137.     }()
  138.     for v := range canal {
  139.         if v.Error != nil {
  140.             log.Println(v)
  141.             continue
  142.         }
  143.  
  144.         fmt.Println("En la página", v.Endpoint, "se han encontrado los siguientes resultados:")
  145.         for _, resultado := range v.Resultados {
  146.             fmt.Println("===>", resultado.Titulo)
  147.             fmt.Println(strings.Replace(resultado.Descripcion, `\n`, "\n", -1))
  148.             fmt.Println(resultado.Precio)
  149.             fmt.Println(resultado.URL)
  150.         }
  151.         fmt.Println("==========================================")
  152.     }
  153.  
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement