package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
)
// Βρες τον αριθμό των σελίδων
func getPageNumber(page string) int {
lastPage := 0
// Request the HTML page.
res, err := http.Get(page)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
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) {
s.Find("a").Each(func(i int, s *goquery.Selection) {
link, ok := s.Attr("href")
if ok {
title := s.Text()
if strings.Contains(title, "...") {
sliceLink := strings.Split(link, "/")
s := sliceLink[len(sliceLink)-2]
// string to int
lastPage, _ = strconv.Atoi(s)
}
}
})
})
return lastPage
}
func ExampleScrape(page string) {
// Request the HTML page.
res, err := http.Get(page)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
doc.Find("body div.eg-container-outer div.eg-container ul.eg-list").Each(func(i int, s *goquery.Selection) {
s.Find("a").Each(func(i int, s *goquery.Selection) {
link, ok := s.Attr("href")
if ok {
title := s.Text()
fmt.Printf("%s [%s]\n", title, link)
}
})
})
}
func GetDownloadLink(page string) {
// Request the HTML page.
res, err := http.Get(page)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
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) {
// test, _ := s.Find("div.col-md-12.col-lg-4.px-3.mb-3").Html()
//fmt.Println(s.Html())
action, err := s.Html()
if err == nil {
println("Works" + action)
sliceLink := strings.Split(action, "=")
println(sliceLink[1])
}
})
}
func downloadFile(filepath string, url string) (err error) {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func main() {
// page := "https://www.emulatorgames.net/roms/gameboy/"
// var link string
// lastpage := getPageNumber(page)
// for i := 1; i <= lastpage; i++ {
// if i == 1 {
// link = "https://www.emulatorgames.net/roms/gameboy/"
// } else {
// link = fmt.Sprintf("%s%d/", page, i)
// }
// ExampleScrape(link)
// }
// Get the download link:
GetDownloadLink("https://www.emulatorgames.net/roms/gameboy/gb-pachislot-hissyouhou-jr/")
// Download
// var filename string = "urls.zip"
// var url1 string = "https://static.emulatorgames.net/roms/gameboy/Akumajou Dracula - Shikkokutaru Zensoukyoku (J) [S][!].zip"
//downloadFile(filename, url1)
}
Comments