I don't know get Title insert to mongo if it is solution your
Full code :
package main
import (
"fmt"
"log"
"net/http"
"gopkg.in/mgo.v2"
"github.com/PuerkitoBio/goquery"
"time"
)
type Data struct{
Name string ` bson:"name"`
Away string ` bson:"away"`
CreatedAt time.Time `bson:"createdAt"`
}
func ExampleScrape() {
// Request the HTML page.
session, err := mgo.Dial("mongodb://127.0.0.1:27017")
res, err := http.Get(
"https://www.edtguide.com/place/%E0%B8%AB%E0%B8%A1%E0%B8%A7%E0%B8%94%E0%B9%82%E0%B8%A3%E0%B8%87%E0%B8%9E%E0%B8%A2%E0%B8%B2%E0%B8%9A%E0%B8%B2%E0%B8%A5/%E0%B8%AA%E0%B8%A1%E0%B8%B8%E0%B8%97%E0%B8%A3%E0%B8%9B%E0%B8%A3%E0%B8%B2%E0%B8%81%E0%B8%B2%E0%B8%A3/view/P0"
)
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)
}
// Find the review items
doc.Find(".archive-list article ").Each(func(i int, s *goquery.Selection) {
// For each item found, get the band and title
band := s.Find("p").Text()
title := s.Find("h2").Text()
fmt.Printf("Review %d: %s - %s\n", i,band, title)
// var data []Data
session.SetMode(mgo.Monotonic, true)
d:= session.DB("api").C("api")
doc := Data{
Name: title,
Away: band,
}
err = d.Insert(doc)
if err != nil {
panic(err)
}
})
}
func main() {
ExampleScrape()
}
เมื่อ วันพุธที่ 9 มกราคม ค.ศ. 2019 18 นาฬิกา 21 นาที 01 วินาที UTC+7,
Mahendra Bhoir เขียนว่า:
>
> Hi,
> This code works for me.
> You need to pass referance instead of actual variable.
>
> //here mongo is my package where function MongoConnect() creates a globle
> session and returns clone of session.
>
> func addTempCategory(category MainCategory) {
> mongoSession = mongo.MongoConnect()
> defer mongoSession.Close()
> c := mongo.MongoSession.DB("testDB").C("testCategories")
>
> err := c.Insert(&category)
> if err != nil {
> log.Fatal(err)
> }
> }
>
> And struct for category
>
> type MainCategory struct {
> Category string `bson:"category" json:"category"`
> CategoryID string `bson:"categoryId" json:"categoryId"`
> CategoryName string `bson:"categoryName" json:"categoryName"`
> HaveAttributes int `bson:"haveAttributes" json:"haveAttributes"`
> HaveSubCats int `bson:"haveSubCats" json:"haveSubCats"`
> ImageIcon string `bson:"imageIcon" json:"imageIcon"`
> }
>
> On Wednesday, January 9, 2019 at 4:29:39 PM UTC+5:30, [email protected]
> wrote:
>>
>> My code:
>> session.SetMode(mgo.Monotonic, true)
>> d:= session.DB("api").C("api")
>> doc := Data{
>> Name: title,
>> Away: band,
>> }
>> err = d.Insert(doc)
>> json to expect
>> [
>> {
>> "contentData": "",
>> "historyData": " ",
>>
>> },
>> {
>> },
>> ]
>>
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.