Advertisement
Guest User

PA

a guest
Jul 17th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.81 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "log"
  6.     "math"
  7.     "net/http"
  8.     "os"
  9.     "regexp"
  10.     "strconv"
  11.     "strings"
  12.  
  13.     "github.com/PuerkitoBio/goquery"
  14.     "github.com/tealeg/xlsx"
  15. )
  16.  
  17. const mainUri string = "http://www.paginasamarillas.com.ar/buscar/r/motores-electricos-antiexplosivos/"
  18. const xlsxFileName string = "PaginasAmarillas_Categoría_MotoresElectricosAntiexplosivos.xlsx"
  19.  
  20. func main() {
  21.  
  22.     finalUri := mainUri
  23.     cn := 1
  24.     var totalPages int
  25.     var file *xlsx.File
  26.     var sheet *xlsx.Sheet
  27.     var row *xlsx.Row
  28.     var cell *xlsx.Cell
  29.     var err error
  30.  
  31.     file = xlsx.NewFile()
  32.     sheet, err = file.AddSheet("Sheet1")
  33.     if err != nil {
  34.         fmt.Printf(err.Error())
  35.     }
  36.  
  37.     for {
  38.  
  39.         res, err := http.Get(finalUri)
  40.  
  41.         if res.Status == "404 ACT" || (cn > totalPages && totalPages > 0) /*|| cn >= 2 */ {
  42.             break
  43.         }
  44.  
  45.         if err != nil {
  46.             log.Fatal(err)
  47.         }
  48.  
  49.         defer res.Body.Close()
  50.  
  51.         doc, err := goquery.NewDocumentFromReader(res.Body)
  52.  
  53.         if err != nil {
  54.             log.Fatal(err)
  55.         }
  56.  
  57.         if cn == 1 {
  58.  
  59.             totalResults := doc.Find("ul.breadcrumb-links li").Eq(-1).Text()
  60.             re := regexp.MustCompile("[0-9]+")
  61.             totalResultsInt, err := strconv.Atoi(re.FindString(totalResults))
  62.             if err != nil {
  63.                 fmt.Println(err)
  64.                 os.Exit(2)
  65.             }
  66.             d := float64(totalResultsInt) / float64(25)
  67.             totalPages = int(math.Ceil(d))
  68.             fmt.Println(totalPages)
  69.  
  70.         }
  71.  
  72.         doc.Find(".col-center ul.businesses li.business").Each(func(i int, s *goquery.Selection) {
  73.  
  74.             row = sheet.AddRow()
  75.  
  76.             itemUri, chkDataHrefBool := s.Attr("data-href")
  77.             name := strings.TrimSpace(s.Find("h2.business-name").Text())
  78.             description := strings.TrimSpace(s.Find("div[itemprop='description']").Text())
  79.             street := strings.TrimSpace(s.Find("p.business-address span").Eq(0).Text())
  80.             address := strings.TrimSpace(s.Find("p.business-address span").Eq(1).Text())
  81.             web := strings.TrimSpace(s.Find("a.business-web").Text())
  82.             mainPhone := strings.TrimSpace(s.Find("div[itemprop='telephone']").Text())
  83.             /*logo, chkDataLogoBool := s.Find("div[itemprop='logo'] .logoad").Attr("src")
  84.             if chkDataLogoBool != false {
  85.                 logo = strings.TrimSpace(logo)
  86.                 fmt.Println(logo)
  87.             }*/
  88.  
  89.             name = trimUnnecessaryWhiteSpaces(name)
  90.             addCellWithValue(name, row, true)
  91.  
  92.             description = trimUnnecessaryWhiteSpaces(description)
  93.             addCellWithValue(description, row, true)
  94.  
  95.             street = trimUnnecessaryWhiteSpaces(street)
  96.             addCellWithValue(street, row, true)
  97.  
  98.             address = trimUnnecessaryWhiteSpaces(address)
  99.             addCellWithValue(address, row, true)
  100.  
  101.             web = trimUnnecessaryWhiteSpaces(web)
  102.             addCellWithValue(web, row, true)
  103.  
  104.             cell = row.AddCell()
  105.             if chkDataHrefBool != false {
  106.                 itemUri = strings.TrimSpace(itemUri)
  107.                 fmt.Println(itemUri)
  108.                 cell.Value = itemUri
  109.             } else {
  110.                 cell.Value = " - "
  111.             }
  112.  
  113.             mainPhone = trimUnnecessaryWhiteSpaces(mainPhone)
  114.             addCellWithValue(mainPhone, row, true)
  115.  
  116.             s.Find("a[itemprop='telephone']").Each(func(i int, ss *goquery.Selection) {
  117.                 morePhones := trimUnnecessaryWhiteSpaces(ss.Text())
  118.                 addCellWithValue(morePhones, row, true)
  119.             })
  120.             fmt.Println("**** ")
  121.         })
  122.  
  123.         cn = cn + 1
  124.  
  125.         finalUri = mainUri + "p-" + strconv.Itoa(cn) + "/"
  126.  
  127.         fmt.Println(" ----------------------------- ")
  128.  
  129.     }
  130.  
  131.     err = file.Save(xlsxFileName)
  132.     if err != nil {
  133.         fmt.Printf(err.Error())
  134.     }
  135.  
  136. }
  137.  
  138. func trimUnnecessaryWhiteSpaces(str string) string {
  139.     re_leadclose_whtsp := regexp.MustCompile(`^[\s\p{Zs}]+|[\s\p{Zs}]+$`)
  140.     re_inside_whtsp := regexp.MustCompile(`[\s\p{Zs}]{2,}`)
  141.     final := re_leadclose_whtsp.ReplaceAllString(str, "")
  142.     str = re_inside_whtsp.ReplaceAllString(final, " ")
  143.     return strings.TrimSpace(str)
  144. }
  145.  
  146. func addCellWithValue(str string, row *xlsx.Row, print bool) {
  147.     cell := row.AddCell()
  148.     if str != "" {
  149.         cell.Value = str
  150.     } else {
  151.         cell.Value = " - "
  152.     }
  153.     if print {
  154.         fmt.Println(str)
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement