drpanwe

Untitled

Sep 7th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.60 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "io"
  6.     "log"
  7.     "net/http"
  8.     "os"
  9.     "strconv"
  10.     "strings"
  11.  
  12.     "github.com/PuerkitoBio/goquery"
  13. )
  14.  
  15. // Βρες τον αριθμό των σελίδων
  16. func getPageNumber(page string) int {
  17.     lastPage := 0
  18.  
  19.     // Request the HTML page.
  20.     res, err := http.Get(page)
  21.     if err != nil {
  22.         log.Fatal(err)
  23.     }
  24.     defer res.Body.Close()
  25.     if res.StatusCode != 200 {
  26.         log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
  27.     }
  28.  
  29.     // Load the HTML document
  30.     doc, err := goquery.NewDocumentFromReader(res.Body)
  31.     if err != nil {
  32.         log.Fatal(err)
  33.     }
  34.  
  35.     doc.Find("body div.eg-container-outer div ul.pagination.pagination-lg.justify-content-center.flex-wrap.m-0.mx-3.pb-4").Each(func(i int, s *goquery.Selection) {
  36.         s.Find("a").Each(func(i int, s *goquery.Selection) {
  37.             link, ok := s.Attr("href")
  38.             if ok {
  39.                 title := s.Text()
  40.                 if strings.Contains(title, "...") {
  41.                     sliceLink := strings.Split(link, "/")
  42.                     s := sliceLink[len(sliceLink)-2]
  43.                     // string to int
  44.                     lastPage, _ = strconv.Atoi(s)
  45.                 }
  46.             }
  47.         })
  48.     })
  49.     return lastPage
  50. }
  51.  
  52. func ExampleScrape(page string) {
  53.     // Request the HTML page.
  54.     res, err := http.Get(page)
  55.     if err != nil {
  56.         log.Fatal(err)
  57.     }
  58.     defer res.Body.Close()
  59.     if res.StatusCode != 200 {
  60.         log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
  61.     }
  62.  
  63.     // Load the HTML document
  64.     doc, err := goquery.NewDocumentFromReader(res.Body)
  65.     if err != nil {
  66.         log.Fatal(err)
  67.     }
  68.  
  69.     doc.Find("body div.eg-container-outer div.eg-container ul.eg-list").Each(func(i int, s *goquery.Selection) {
  70.         s.Find("a").Each(func(i int, s *goquery.Selection) {
  71.             link, ok := s.Attr("href")
  72.             if ok {
  73.                 title := s.Text()
  74.  
  75.                 fmt.Printf("%s [%s]\n", title, link)
  76.             }
  77.         })
  78.     })
  79. }
  80.  
  81. func GetDownloadLink(page string) {
  82.     // Request the HTML page.
  83.     res, err := http.Get(page)
  84.     if err != nil {
  85.         log.Fatal(err)
  86.     }
  87.     defer res.Body.Close()
  88.     if res.StatusCode != 200 {
  89.         log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
  90.     }
  91.  
  92.     // Load the HTML document
  93.     doc, err := goquery.NewDocumentFromReader(res.Body)
  94.     if err != nil {
  95.         log.Fatal(err)
  96.     }
  97.  
  98.     doc.Find("body div.eg-container.pt-0.pt-sm-3 div.eg-expand.row div.col-md-12.col-lg-4.px-3.mb-3").Each(func(i int, s *goquery.Selection) {
  99.         // test, _ := s.Find("div.col-md-12.col-lg-4.px-3.mb-3").Html()
  100.         //fmt.Println(s.Html())
  101.         action, err := s.Html()
  102.         if err == nil {
  103.             println("Works" + action)
  104.             sliceLink := strings.Split(action, "=")
  105.             println(sliceLink[1])
  106.         }
  107.     })
  108. }
  109.  
  110. func downloadFile(filepath string, url string) (err error) {
  111.  
  112.     // Create the file
  113.     out, err := os.Create(filepath)
  114.     if err != nil {
  115.         return err
  116.     }
  117.     defer out.Close()
  118.  
  119.     // Get the data
  120.     resp, err := http.Get(url)
  121.     if err != nil {
  122.         return err
  123.     }
  124.     defer resp.Body.Close()
  125.  
  126.     // Writer the body to file
  127.     _, err = io.Copy(out, resp.Body)
  128.     if err != nil {
  129.         return err
  130.     }
  131.  
  132.     return nil
  133. }
  134.  
  135. func main() {
  136.     // page := "https://www.emulatorgames.net/roms/gameboy/"
  137.     // var link string
  138.     // lastpage := getPageNumber(page)
  139.     // for i := 1; i <= lastpage; i++ {
  140.     //  if i == 1 {
  141.     //      link = "https://www.emulatorgames.net/roms/gameboy/"
  142.     //  } else {
  143.     //      link = fmt.Sprintf("%s%d/", page, i)
  144.     //  }
  145.     //  ExampleScrape(link)
  146.     // }
  147.  
  148.     // Get the download link:
  149.  
  150.     GetDownloadLink("https://www.emulatorgames.net/roms/gameboy/gb-pachislot-hissyouhou-jr/")
  151.  
  152.     // Download
  153.     // var filename string = "urls.zip"
  154.     // var url1 string = "https://static.emulatorgames.net/roms/gameboy/Akumajou Dracula - Shikkokutaru Zensoukyoku (J) [S][!].zip"
  155.  
  156.     //downloadFile(filename, url1)
  157. }
  158.  
Add Comment
Please, Sign In to add comment