Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "log"
- "net/http"
- "regexp"
- "strings"
- "sync"
- )
- type Resultado struct {
- Titulo string
- Descripcion string
- URL string
- Precio string
- }
- type PaginaMA struct {
- Error error
- Endpoint string
- Resultados []Resultado
- }
- type RespuestaMilAnunciosMin struct {
- AdList struct {
- Ads []struct {
- Description string `json:"description"`
- Price struct {
- CashPrice struct {
- Text string `json:"text"`
- } `json:"cashPrice"`
- } `json:"price"`
- Title string `json:"title"`
- URL string `json:"url"`
- } `json:"ads"`
- } `json:"adList"`
- }
- var wg sync.WaitGroup
- func scrap_ma(cliente *http.Client, endpointMA *string, canal chan PaginaMA) chan PaginaMA {
- defer wg.Done()
- var resultados []Resultado
- peticionMA, errorPeticionMA := http.NewRequest("GET", *endpointMA, nil)
- if errorPeticionMA != nil {
- canal <- PaginaMA{
- Error: errorPeticionMA,
- Endpoint: *endpointMA,
- }
- return canal
- }
- respuestaMA, errorRespuestaMA := cliente.Do(peticionMA)
- if errorRespuestaMA != nil {
- canal <- PaginaMA{
- Error: errorPeticionMA,
- Endpoint: *endpointMA,
- }
- return canal
- }
- defer respuestaMA.Body.Close()
- contenidoPagina, errorContenidoPagina := ioutil.ReadAll(respuestaMA.Body)
- if errorContenidoPagina != nil {
- canal <- PaginaMA{
- Error: errorContenidoPagina,
- Endpoint: *endpointMA,
- }
- return canal
- }
- html := string(contenidoPagina)
- expRegMin := regexp.MustCompile(`\n`)
- html = expRegMin.ReplaceAllString(html, "")
- expRegAds := regexp.MustCompile(`.*?<script>window\.__INITIAL_.*?adListPagination...`)
- html = expRegAds.ReplaceAllString(html, "")
- expRegPag := regexp.MustCompile(`.."pagination.*`)
- html = expRegPag.ReplaceAllString(html, "")
- expRegularEscapeComillas := regexp.MustCompile(`\\\\\\"`)
- html = expRegularEscapeComillas.ReplaceAllString(html, `#_#_#_#_#"`)
- expRegularComillas := regexp.MustCompile(`\\"`)
- html = expRegularComillas.ReplaceAllString(html, `"`)
- caracteresEscape := regexp.MustCompile(`#_#_#_#_#"`)
- html = caracteresEscape.ReplaceAllString(html, `\"`)
- html = html + "}"
- var respuestaMilAnunciosMin RespuestaMilAnunciosMin
- errorConverJSON := json.Unmarshal([]byte(html), &respuestaMilAnunciosMin)
- if errorConverJSON != nil {
- canal <- PaginaMA{
- Error: errorConverJSON,
- Endpoint: *endpointMA,
- }
- return canal
- }
- for _, v := range respuestaMilAnunciosMin.AdList.Ads {
- var resultado Resultado
- resultado.Titulo = v.Title
- resultado.Descripcion = v.Description
- resultado.Precio = v.Price.CashPrice.Text
- resultado.URL = "https://www.milanuncios.com" + v.URL
- resultados = append(resultados, resultado)
- }
- canal <- PaginaMA{
- Error: nil,
- Endpoint: *endpointMA,
- Resultados: resultados,
- }
- return canal
- }
- func main() {
- cliente := http.Client{}
- canal := make(chan PaginaMA)
- for i := 1; i < 11; i++ {
- endpointMA := fmt.Sprint("https://www.milanuncios.com/portatiles-de-segunda-mano/?pagina=", i)
- wg.Add(1)
- go scrap_ma(&cliente, &endpointMA, canal)
- }
- go func() {
- wg.Wait()
- close(canal)
- }()
- for v := range canal {
- if v.Error != nil {
- log.Println(v)
- continue
- }
- fmt.Println("En la página", v.Endpoint, "se han encontrado los siguientes resultados:")
- for _, resultado := range v.Resultados {
- fmt.Println("===>", resultado.Titulo)
- fmt.Println(strings.Replace(resultado.Descripcion, `\n`, "\n", -1))
- fmt.Println(resultado.Precio)
- fmt.Println(resultado.URL)
- }
- fmt.Println("==========================================")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement