colly源码包分析

基本用法 infos := make([]info, 0) c := colly.NewCollector() c.OnHTML(".content div .rprt", func(e *colly.HTMLElement) { title := e.ChildText(".rslt p.title a") author := e.ChildText(".supp .desc") message := e.ChildText(".supp .details") pmid := e.ChildText(".aux .resc .rprtid dd") id, _ := strconv.ParseInt(pmid, 10, 64) i := info{ title: strings.TrimRight(title, "."), author: author, message: message, pmid: id, } infos = append(infos, i) }) c.OnRequest(func(req *colly.Request) { log.Println("Visit:", req.URL.String()) }) c.Post("https://www.ncbi.nlm.nih.gov/pubmed", map[string]string{ "term": "kif15", "p$a": strconv.

Read More →

golang标准包分析—http

服务端 服务端是用来接收连接的,因此需要一个监听的逻辑 // 函数ListenAndServe用来开启服务并且监听,而这个 // 函数的核心就是tcp连接,通过net.Listen(param1,param2)得到listener // 其中param1是连接类型,可以有tcp,tcp4,tcp6......,param2则是服务地址:ip:port // 返回的listener是一个interface{}类型 type Listener interface { // Accept waits for and returns the next connection to the listener. Accept() (Conn, error) // Close closes the listener. // Any blocked Accept operations will be unblocked and return errors. Close() error // Addr returns the listener's network address. Addr() Addr } 只要实现了Listener的三个函数的结构体,都能作为返回参数,以param1=tcp为例子,将会返回TCPListener // TCPListener is a TCP network listener. Clients should typically // use variables of type Listener instead of assuming TCP.

Read More →