转移到新main.go上

This commit is contained in:
eson 2023-06-18 13:56:59 +08:00
parent e6f0ccd662
commit 61108f1110
2 changed files with 75 additions and 67 deletions

73
main.go Normal file
View File

@ -0,0 +1,73 @@
package main
import (
"log"
"os"
"strconv"
"strings"
"github.com/474420502/extractor"
"github.com/474420502/requests"
)
type ItemList struct {
LogoUri []string `exp:"//div[@class='result-group bg-white']//img" method:"AttributeValue,src"`
}
func main() {
ses := requests.NewSession()
tp := ses.Get("https://brandeps.com/logo/Latest?page=500")
dp, err := os.ReadFile("logos/.record")
if err != nil {
log.Panic(err)
}
startPage, err := strconv.Atoi(string(dp))
if err != nil {
log.Panic(err)
}
page := tp.QueryParam("page")
for i := startPage; i < 500; i++ {
log.Println("page", i)
// strconv.Itoa(i)
err = os.WriteFile("logos/.record", []byte(strconv.Itoa(i)), 0644)
if err != nil {
panic(err)
}
page.IntSet(int64(i))
res, err := tp.Execute()
if err != nil {
panic(err)
}
// log.Println(res.ContentString())
etor := extractor.ExtractHtml(res.Content())
itemlist := &ItemList{}
etor.GetObjectByTag(itemlist)
if len(itemlist.LogoUri) <= 0 {
break
}
for _, uri := range itemlist.LogoUri {
// uri = strings.Replace(uri, ".svg", ".eps", 1)
log.Println(uri)
res, err = ses.Get(uri).Execute()
if err != nil {
panic(err)
}
index := strings.LastIndex(uri, "/")
if index >= 0 {
log.Println(uri[index+1:])
err = os.WriteFile("logos/"+uri[index+1:], res.Content(), 0644)
if err != nil {
panic(err)
}
}
}
}
// https://brandeps.com/logo/Latest?page=4
}

View File

@ -1,75 +1,10 @@
package downloadlogo
package main
import (
"log"
"os"
"strconv"
"strings"
"testing"
"github.com/474420502/extractor"
"github.com/474420502/requests"
)
type ItemList struct {
LogoUri []string `exp:"//div[@class='result-group bg-white']//img" method:"AttributeValue,src"`
}
func TestMain(t *testing.T) {
ses := requests.NewSession()
tp := ses.Get("https://brandeps.com/logo/Latest?page=500")
dp, err := os.ReadFile("logos/.record")
if err != nil {
log.Panic(err)
}
startPage, err := strconv.Atoi(string(dp))
if err != nil {
log.Panic(err)
}
page := tp.QueryParam("page")
for i := startPage; i < 500; i++ {
log.Println("page", i)
// strconv.Itoa(i)
err = os.WriteFile("logos/.record", []byte(strconv.Itoa(i)), 0644)
if err != nil {
panic(err)
}
page.IntSet(int64(i))
res, err := tp.Execute()
if err != nil {
panic(err)
}
// log.Println(res.ContentString())
etor := extractor.ExtractHtml(res.Content())
itemlist := &ItemList{}
etor.GetObjectByTag(itemlist)
if len(itemlist.LogoUri) <= 0 {
break
}
for _, uri := range itemlist.LogoUri {
// uri = strings.Replace(uri, ".svg", ".eps", 1)
log.Println(uri)
res, err = ses.Get(uri).Execute()
if err != nil {
panic(err)
}
index := strings.LastIndex(uri, "/")
if index >= 0 {
log.Println(uri[index+1:])
err = os.WriteFile("logos/"+uri[index+1:], res.Content(), 0644)
if err != nil {
panic(err)
}
}
}
}
// https://brandeps.com/logo/Latest?page=4
main()
}