scope/search/web/bing.go

98 lines
1.9 KiB
Go
Raw Normal View History

2021-12-08 17:24:05 +00:00
package web
import (
"net/http"
"strconv"
"github.com/PuerkitoBio/goquery"
)
var bingURL = urlMustParse("https://www.bing.com/search?count=10")
type Bing struct {
keyword string
userAgent string
first int
doc *goquery.Document
initDone bool
baseSel *goquery.Selection
}
func (b *Bing) SetKeyword(keyword string) {
b.keyword = keyword
}
func (b *Bing) SetPage(page int) {
b.first = page * 10
}
func (b *Bing) SetUserAgent(ua string) {
b.userAgent = ua
}
func (b *Bing) Init() error {
initURL := copyURL(bingURL)
query := initURL.Query()
query.Set("q", b.keyword)
if b.first > 0 {
query.Set("first", strconv.Itoa(b.first))
} else {
query.Set("first", "1")
}
initURL.RawQuery = query.Encode()
req, err := http.NewRequest(
http.MethodGet,
initURL.String(),
nil,
)
if err != nil {
return err
}
if b.userAgent == "" {
b.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
}
req.Header.Set("User-Agent", b.userAgent)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return err
}
b.doc = doc
b.baseSel = doc.Find(`#b_results > li`)
b.initDone = true
return nil
}
func (b *Bing) Each(eachCb func(int) error) error {
for i := 0; i < b.baseSel.Length(); i++ {
err := eachCb(i)
if err != nil {
return err
}
}
return nil
}
func (b *Bing) Title(i int) (string, error) {
return get(b.baseSel, i).ChildrenFiltered("h2").Children().First().Text(), nil
}
func (b *Bing) Link(i int) (string, error) {
return get(b.baseSel, i).ChildrenFiltered("h2").Children().First().AttrOr("href", ""), nil
}
func (b *Bing) Desc(i int) (string, error) {
return get(b.baseSel, i).ChildrenFiltered(".b_caption").Children().Last().Text(), nil
}
func (b *Bing) Name() string {
return "bing"
}