GO语言的进阶之路-协程和Channel
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
看过我之前几篇博客小伙伴可能对Golang语言的语法上了解的差不多了,但是,如果想要你的代码和性能更高,那还得学点晋升的本来,这个时候我们就需要引入Golang的协成这个概念了,其实,你可能早就听说了Golang的优势就是处理大并发,我们可以用它来做日志收集系统,也可以用它做业务上的“秒杀系统”,当然我们还可以用它来做“监控系统”。好了,下面跟我一起来体会一下Golang的五味杂陈吧。
一.什么是协程;
再说协成之前,我们需要了解两个概念,即用户态和内核态。
1.什么是用户态;
官方解释:用户态(user mode)在计算机结构指两项类似的概念。在CPU的设计中,用户态指非特权状态。在此状态下,执行的代码被硬件限定,不能进行某些操作,比如写入其他进程的存储空间,以防止给操作系统带来安全隐患。在操作系统的设计中,用户态也类似,指非特权的执行状态。内核禁止此状态下的代码进行潜在危险的操作,比如写入系统配置文件、杀掉其他用户的进程、重启系统等。
应用程序在用户态下运行,仅仅只能执行cpu整个指令集的一个子集,该子集中不包含操作硬件功能的部分,因此,一般情况下,在用户态中有关I/O和内存保护(操作系统占用的内存是受保护的,不能被别的程序占用)。
如果感兴趣的朋友可以参考:https://baike.baidu.com/item/%E7%94%A8%E6%88%B7%E6%80%81/9548791?fr=aladdin
2.什么是内核态;
内核态也叫和核心态。
官方解释:在处理器的存储保护中,主要有两种权限状态,一种是核心态(管态),也被称为特权态;一种是用户态(目态)。核心态是操作系统内核所运行的模式,运行在该模式的代码,可以无限制地对系统存储、外部设备进行访问。
操作系统在内核态运行情况下可以访问硬件上所有的内容。
如果感兴趣的朋友可以参考:https://baike.baidu.com/item/%E6%A0%B8%E5%BF%83%E6%80%81/6845908?fr=aladdin
3.什么是协成;
官方解释:一个程序可以包含多个协程,可以对比与一个进程包含多个线程,因而下面我们来比较协程和线程。我们知道多个线程相对独立,有自己的上下文,切换受系统控制;而协程也相对独立,有自己的上下文,但是其切换由自己控制,由当前协程切换到其他协程由当前协程来控制。
协程(coroutine)是Go语言中的轻量级线程实现,由Go运行时(runtime)管理。在一个函数调用前加上go关键字,这次调用就会在一个新的goroutine中并发执行。当被调用的函数返回时,这个goroutine也自动结束。需要注意的是,如果这个函数有返回值,那么这个返回值会被丢弃。协成工作在用户态,它类似于现场的运行方式可以并行处理任务。
二.创建一个协程;
在一个函数调用前加上go关键字,这次调用就会在一个新的goroutine中并发执行,下面我们一起来看段代码的执行结果:
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"time"
"fmt"
) func sayhi(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100*time.Millisecond) //表示每次循环后都要休息100毫秒。
fmt.Println(s)
}
} func main() {
go sayhi("尹正杰") //在函数执行前加个go,表示单独起了一个协程,表示和当前主协程(main)并驾齐驱运行代码。
sayhi("Golang")
} #以上代码执行结果如下:
Golang
尹正杰
尹正杰
Golang
Golang
尹正杰
尹正杰
Golang
Golang
尹正杰
相信大家已经看到了一些端倪,我们先定义了一个“sayhi”函数,这个函数的功能就是将传入的字符串打印5遍,然后我们在主程序上调用的时候,发现在函数面前加"go"关键字时,就会打印输出,但是奇怪的是没有按照顺序打,它没有先打印五遍“尹正杰”然后在打印5遍“Golang”,而是函数执行结果是不规律的打印了两个字符串。这就是并发的效果,两个函数同事运行效果。这也是我们的并发之路的初体验。哈哈~
三.协程的局限性;
发现让两端代码同事运行貌似听起来很高大上的样子,但是有个局限性,就是当主进程结束后,协成也会跟着结束,其实这个很好理解,我们通过一段代码就知道了:
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"time"
"fmt"
) func main() {
s := []int{2,7,1,6,4,3,11,15,17,5,8,9,12}
for _,n := range s{
go func(n int) { //定义一个匿名函数,并对该函数开启协程,每次循环都会开启一个协成,也就是说它开启了13个协程。
time.Sleep(time.Duration(n) * time.Second) /*表示每循环一次就需要睡1s,睡的总时间是由n来控制的,
总长度是由s切片数组中最大的一个数字决定,也就是说这个协成最少需要17秒才会结束哟*/
fmt.Println(n)
}(n) //由于这个函数是匿名函数,所以调用方式就直接:(n)调用,不用输入函数名。
}
time.Sleep(12*time.Second) //主进程要执行的时间是12秒
} #以上代码执行结果如下:
1
2
3
4
5
6
7
8
9
11
12
是不是很神奇,直接就进行排序了,但是15和17没有在终端输出,为什么呢?这就是我所说的主进程结束,那么整个程序也终将结束,因为主程序的时间只有12s,如果你想让所有的数据都排序出来,就得把数字换成一个大于或等于17的,这样才会对所有的数字进行排序。因为协成13个协程都要执行完毕的话需要17s才好使。
四.锁;
1.为什么要引入锁;
在线上生活中,我们为了避免多个用户操作同一个文件,就会定义一个锁,为什么我们需要一个锁呢?我们可以看以下的案例:
package main import (
"time"
"fmt"
) type Accout struct {
money int
} func (a *Accout) Do_Prepare() {
time.Sleep(time.Second)
} func (a *Accout) Get_Gongzi(n int) {
a.money += n
} func (a *Accout) Give_Wife(n int) {
if a.money > n {
a.Do_Prepare()
a.money -= n
}
} func (a *Accout) Buy(n int) {
if a.money >n {
a.Do_Prepare()
a.money -= n
}
} func (a *Accout) Left()int {
return a.money
} func main() {
var account Accout
account.Get_Gongzi(10000)
go account.Give_Wife(6000)
go account.Buy(5000)
time.Sleep(2 * time.Second) //不能让主程序结束掉,因为主进程一结束go的协程也就跟着结束啦。
fmt.Println(account.Left())
} #以上代码执行结果如下:
-1000
估计大家都看出来端倪了,在让两个协程并发跑起来,发现你得到的10000块钱都让给花出去了,是不是老尴尬了,写的判断语句也是没有生效的,最终10000块钱的工资,它竟然花了11000,如果银行这么干早就倒闭了,哈哈~那么问题来了,如果解决这一种现象呢?目前有四种常见的处理方案即:互斥锁,读写锁和channel锁以及waitgroup等等。
2.互斥锁;
互斥锁是传统的并发程序对共享资源进行访问控制的主要手段。它由标准库代码包sync中的Mutex结构体类型代表。sync.Mutex类型(确切地说,是*sync.Mutex类型)只有两个公开方法——Lock和Unlock。顾名思义,前者被用于锁定当前的互斥量,而后者则被用来对当前的互斥量进行解锁。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"time"
"fmt"
"sync"
) type Accout struct {
flag sync.Mutex
money int
} func (a *Accout) Do_Prepare() {
time.Sleep(time.Second)
} func (a *Accout) Get_Salary(n int) { //定义发的工资
a.money += n
} func (a *Accout) Give_Wife(n int) { //上交给妻子的工资
a.flag.Lock()
defer a.flag.Unlock()
if a.money > n {
a.Do_Prepare()
a.money -= n
}else {
fmt.Println("您的余额已不足!请及时充值~")
}
} func (a *Accout) Buy(n int) { //自己买的工资
a.flag.Lock()
defer a.flag.Unlock()
if a.money >n {
a.Do_Prepare()
a.money -= n
}else {
fmt.Println("您的余额已不足!请及时充值~")
}
} func (a *Accout) Left()int {
return a.money
} func main() {
var account Accout
account.Get_Salary(10000)
go account.Give_Wife(6000)
go account.Buy(5000)
time.Sleep(2 * time.Second) //不能让主程序结束掉,因为主进程一结束go的协程也就跟着结束啦。
fmt.Printf("您的剩余工资是\033[31;1m%d\033[0m",account.Left()) } #以上代码解析如下:
您的余额已不足!请及时充值~
您的剩余工资是4000
3.读写锁;
在Go语言中,读写锁由结构体类型sync.RWMutex代表。与互斥锁类似,sync.RWMutex类型的零值就已经是立即可用的读写锁了。
读写锁即是针对于读写操作的互斥锁。它与普通的互斥锁最大的不同就是,它可以分别针对读操作和写操作进行锁定和解锁操作。读写锁控制下的多个写操作之间都是互斥的,并且写操作与读操作之间也都是互斥的。但是,多个读操作之间却不存在互斥关系。
A.多个读操作之间却不存在互斥关系;
我们会发现读取操作没有存在互斥的关系。5个进程同事进行读取,最终都顺利并行跑完了。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"sync"
"time"
) var m *sync.RWMutex //定义m的类型为读写锁。 func main() {
m = new(sync.RWMutex)
go read("第一次读取:") //开启5个协程进行读取操作。如果不能并发跑最少需要5秒钟时间。
go read("第二次读取:")
go read("第三次读取:")
go read("第四次读取:")
go read("第五次读取:")
time.Sleep(2* time.Second) //主进程只有2s,意味着这个程序最多能运行2s的时间。
} func read(i string) {
println(i, "read start")
m.RLock() //读锁定
println(i, "资料正在读取中....")
time.Sleep(1 * time.Second) //改函数执行完毕最少需要睡1秒。
m.RUnlock() //读解锁
println(i, "read end")
} #以上代码执行结果如下:
第二次读取: read start
第二次读取: 资料正在读取中....
第一次读取: read start
第一次读取: 资料正在读取中....
第三次读取: read start
第三次读取: 资料正在读取中....
第四次读取: read start
第四次读取: 资料正在读取中....
第五次读取: read start
第五次读取: 资料正在读取中....
第一次读取: read end
第三次读取: read end
第二次读取: read end
第四次读取: read end
第五次读取: read end
B.读写锁控制下的多个写操作之间都是互斥的,并且写操作与读操作之间也都是互斥的;
我们发现,读的时候不能写,写的时候不能读,我们将主程序设置的时间是10秒钟,一次读两次写花费的总时间是12秒钟,因此肯定有一个是无法完成度或是写的部分。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"sync"
"time"
) var m *sync.RWMutex func read(i string) {
println(i, "read start")
m.RLock()
println(i, "资料正在读取中.....")
time.Sleep(4 * time.Second)
m.RUnlock()
println(i, "read end")
} func write(i string) {
println(i, "write start")
m.Lock() //写操作锁定
println(i, "数据正在写入硬盘中.....")
time.Sleep(4 * time.Second)
m.Unlock() //写操作解锁
println(i, "write end")
} func main(){
m = new(sync.RWMutex)
go write("第一次写入") //写的时候啥都不能干,即其他协程无法写入,也无法读取。
go read("第一次读取")
go write("第二次写入")
time.Sleep(10 * time.Second) //主进程只给出10秒的时间,但是整个进程跑完最少需要12秒的时间。
} #以上代码直接结果如下:
第一次写入 write start
第一次写入 数据正在写入硬盘中.....
第一次读取 read start
第二次写入 write start
第一次写入 write end
第一次读取 资料正在读取中.....
第一次读取 read end
第二次写入 数据正在写入硬盘中.....
4.channel锁;
channel锁的工作原理就是让2个协成互相通信,一会我们会详细介绍。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"time"
"fmt"
"sync"
) type Accout struct {
flag sync.Mutex
money int
} func (a *Accout) Do_Prepare() {
time.Sleep(time.Second)
} func (a *Accout) Get_Gongzi(n int) {
a.money += n
} func (a *Accout) Give_Wife(n int) {
a.flag.Lock()
defer a.flag.Unlock()
if a.money > n {
a.Do_Prepare()
a.money -= n
}else {
fmt.Println("您的余额已不足,请及时充值!")
}
} func (a *Accout) Buy(n int) {
a.flag.Lock()
defer a.flag.Unlock()
if a.money >n {
a.Do_Prepare()
a.money -= n
}else {
fmt.Println("您的余额已不足,请及时充值!")
}
} func (a *Accout) Left()int {
return a.money
} func main() {
var account Accout
account.Get_Gongzi(10000) var work_info chan string
work_info = make(chan string ,2) //定义一个channel go func() {
account.Give_Wife(6000)
work_info <- "I done" //如果该进程执行完毕就发送一条数据给channel,下面的那个也一样
}() go func() {
account.Buy(50000)
work_info <- "I have done too!"
}() cnt := 0
for i := range work_info {
fmt.Println(i)
cnt++ //每次循环都叠加1,当2个协程都工作完就让主程序结束。
if cnt >= 2 {
break
} }
defer close(work_info)
fmt.Printf("您的剩余工资是\033[31;1m%d\033[0m",account.Left())
} #以上代码直接结果如下:
您的余额已不足,请及时充值!
I have done too!
I done
您的剩余工资是4000
其实channel还有一个好处就是解决超时问题,就是如果在规定的时间没有完成进程的内容,我们就返回给用户超时的状态。以下是示例代码:
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"time"
"fmt"
"sync"
) type Accout struct {
flag sync.Mutex
money int
} func (a *Accout) Do_Prepare() {
time.Sleep(time.Second)
} func (a *Accout) Get_Gongzi(n int) {
a.money += n
} func (a *Accout) Give_Wife(n int) {
a.flag.Lock()
defer a.flag.Unlock()
if a.money > n {
a.Do_Prepare()
a.money -= n
}else {
fmt.Println("您的余额已不足,请及时充值!")
}
} func (a *Accout) Buy(n int) {
a.flag.Lock()
defer a.flag.Unlock()
if a.money >n {
a.Do_Prepare()
a.money -= n
}else {
fmt.Println("您的余额已不足,请及时充值!")
}
} func (a *Accout) Left()int {
return a.money
} func main() {
var account Accout
account.Get_Gongzi(10000) var work_info chan string
work_info = make(chan string ,2) //定义一个channel go func() {
account.Give_Wife(6000)
work_info <- "I done" //如果该进程执行完毕就发送一条数据给channel,下面的那个也一样
}() go func() {
account.Buy(50000)
work_info <- "I have done too!"
}() cnt := 0
for i := range work_info {
fmt.Println(i)
cnt++ //每次循环都叠加1,当2个协程都工作完就让主程序结束。
if cnt >= 2 {
fmt.Println("操作超时")
return
} }
defer close(work_info)
fmt.Printf("您的剩余工资是\033[31;1m%d\033[0m",account.Left())
} #以上代码执行结果如下:
您的余额已不足,请及时充值!
I have done too!
I done
操作超时
5.waitgroup进行同步;
WaitGroup在go语言中,用于线程同步,它能够一直等到所有的goroutine执行完成,并且阻塞主线程的执行,直到所有的goroutine执行完成。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"time"
"fmt"
"sync"
) type Accout struct {
flag sync.Mutex
money int
} func (a *Accout) Do_Prepare() {
time.Sleep(time.Second)
} func (a *Accout) Get_Gongzi(n int) {
a.money += n
} func (a *Accout) Give_Wife(n int) {
a.flag.Lock()
defer a.flag.Unlock()
if a.money > n {
a.Do_Prepare()
a.money -= n
}else {
fmt.Println("您的余额已不足,请及时充值!")
}
} func (a *Accout) Buy(n int) {
a.flag.Lock()
defer a.flag.Unlock()
if a.money >n {
a.Do_Prepare()
a.money -= n
}else {
fmt.Println("您的余额已不足,请及时充值!")
}
} func (a *Accout) Left()int {
return a.money
} func main() {
var account Accout
account.Get_Gongzi(10000)
wg := new(sync.WaitGroup) //定义一个WaitGroup,就是用来等待都
wg.Add(2) //有几个进程需要等待就写几个,写多或写少都会报错哟~
go func() {
account.Give_Wife(6000)
wg.Done() //该进程结束就发送结束标志。
}()
go func() {
account.Buy(5000)
wg.Done()
}()
wg.Wait() //等待所有协程结束后在执行以下都代码。
fmt.Printf("您的剩余工资是\033[31;1m%d\033[0m",account.Left())
} #以上代码执行结果如下:
您的余额已不足,请及时充值!
您的剩余工资是5000
五.协程池;
协程池可以控制并行度,复用协程。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"sync"
"net/http"
"log"
) func work(ch chan string, wg *sync.WaitGroup) { //ch是用来接受数据,wg是用来同步协程的.
for u := range ch{
resp,err := http.Get(u) //将channel中的数据读取出来
if err != nil {
log.Print(err)
return
}
log.Printf("访问的网站是:%s,网页的大小是:%v",u,resp.ContentLength)
resp.Body.Close() //
}
wg.Done()
} func main() {
wg := new(sync.WaitGroup) //定义WaitGroup为了进行协程同步。
wg.Add(5) //开启5个协程池。
taskch := make(chan string) //创建一个channel
for i := 0; i < 5; i++ {
go work(taskch,wg)
}
urls := []string{"http://www.baidu.com","http://www.zhihu.com","http://www.cnblogs.com/yinzhengjie/p/7201980.html"} //定义需要访问的网站。
for _,url := range urls{
taskch <- url //往channel发送数据。
}
close(taskch)
wg.Wait() //等待程序结束
} #以上代码执行结果如下:
2017/07/24 15:23:02 访问的网站是:http://www.baidu.com,网页的大小是:-1
2017/07/24 15:23:03 访问的网站是:http://www.cnblogs.com/yinzhengjie/p/7201980.html,网页的大小是:-1
2017/07/24 15:23:03 访问的网站是:http://www.zhihu.com,网页的大小是:-1
六.channel的引入;
Go 语言中的 channel 是实现 goroutine 间无锁通信的关键机制,他使得写多线程并发程序变得简单、灵活、触手可得。channel是Go语言在语言级别提供的goroutine间的通信方式,我们可以使用channel在多个goroutine之间传递消息。channel是进程内的通信方式,因此通过channel传递对象的过程和调用函数时的参数传递行为比较一致,比如也可以传递指针等。
1.实现channel的Receive与Sender;
初学者可以理解成:channel实际上就是接受和发送数据。channel是类型相关的,一个channel只能传递一种类型的值,这个类型需要在声明channel时指定。有了这些就够了,我们一起来看段代码来感受下channel吧。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import "fmt" func sum(s []int, c chan int) { //该函数是对切片数组求和,需要传入一个切片数组和一个channel。
sum := 0
for _,v := range s {
sum += v //表示对切片数组进行求和。
}
c <- sum //将sum的数据送给名称为c的channel,也就是说将数组中的数字的值传给channel。给channle赋值的时候,其应该在左边。
} func main() {
Slice_array := []int{1,2,3,4,-9,4,2,0}
channel_name := make(chan int) //用chan定义一个channel对象名称为channel_name,其类型是int。
go sum(Slice_array[:len(Slice_array)/2],channel_name) //在函数上开启协成,注意格式。是在函数前面加个go参数即可。
go sum(Slice_array[len(Slice_array)/2:],channel_name)
x,y := <-channel_name, <-channel_name //将channel_name中的数据接收并复制给x和y,当然你也可以定义两个变量名不同的channle,要注意到是取channel的值时,其应该在右边,
fmt.Println(x,y,x+y)
} #以上代码执行结果如下:
-3 10 7
2.channel的缓冲大小;
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import "fmt" func main() {
ch := make(chan int,5) /*定义缓冲大小容量为5。下面给channel发送了3个值,如果接受当值超过预定义的5时,就会
报错哟!如果你不写的化就会报错:"fatal error: all goroutines are asleep - deadlock!"*/
ch <- 100
ch <- 200
ch <- 300
fmt.Println(<-ch) //第一次取值
fmt.Println(<-ch) //第二次取值
fmt.Println(<-ch) //第三次取值
} #以上代码执行结果如下:
100
200
300
3.channel的应用;
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import "fmt" func fibonacci(n int,c chan int) { //定义斐波拉契数列函数。
x,y := 0,1
for i := 0; i < n; i++ {
c <- x
x,y = y,x+y
if x > 100 { //表示当循环到两个数字之和大于100时就终止循环。
break
}
}
close(c) //关闭channel,如果不关闭就会被锁,出现报错:"deadlock!"
} func main() {
channel_name := make(chan int,15) //定义channel缓冲大小时为15,表示最多存取15个元素。
go fibonacci(cap(channel_name),channel_name)
for i := range channel_name{ //遍历channel_name
fmt.Println(i)
}
} #以上代码执行结果如下:
0
1
1
2
3
5
8
13
21
34
55
89
4.Channeld的select语法;
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import "fmt" func fibonacci(channel_name,quit chan int) { //定义两个channle对象channel_name和quit。
x,y := 0,1
for{
select {
case channel_name <- x: //用channel_name接受数据。
x,y = y,x+y case <-quit: //表示当接收到quit的channel时,就执行以下代码。其实就是实现关闭channel的功能。
fmt.Println("EXIT")
return //函数一退出协程也就跟着退出了
}
}
} func main() {
channel_name := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 11; i++ {
fmt.Println(<-channel_name) //"<-channel_name"表示读取channel_name中的参数。
}
quit<- 100 /*当for循环结束后,我们随便给quit的channel传一个值就可以实现退出函数的功能。我们之前需要
用close(c)来退出发信号的功能,主动权在"fibonacci",而我们现在我们用quit来主动退出协程。*/
}() //这里加个括号是在调用当前函数。
fibonacci(channel_name,quit) //将channel_name和quit传递给fibonacci函数
} #以上代码珍惜结果如下:
0
1
1
2
3
5
8
13
21
34
55
EXIT
5.default语法;
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"time"
"fmt"
) func main() {
tick := time.Tick(1000*time.Millisecond) //也可以这样写:“tick := time.NewTicker(1000*time.Millisecond).C”其中这个点C就是一个channel。
boom := time.After(5000*time.Millisecond)
for {
select {
case <-tick:
fmt.Println("滴答。。。")
case <-boom:
fmt.Println("砰~")
return
default:
fmt.Println("吃一口凉皮")
time.Sleep(500*time.Millisecond)
}
}
} #以上代码执行结果如下:
吃一口凉皮
吃一口凉皮
滴答。。。
吃一口凉皮
吃一口凉皮
滴答。。。
吃一口凉皮
吃一口凉皮
滴答。。。
吃一口凉皮
吃一口凉皮
滴答。。。
吃一口凉皮
吃一口凉皮
砰~
6.channel扩展;
A.time.NewTicker用法展示:
当我们在写一个监控系统的时候,需要实施监控,但是定义巡检时间的话,需要我们自定义巡检周期性。这个时候我们就可以用到一个叫时间设置器的函数,即:time.NewTicker。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"time"
"fmt"
) func main() {
timer := time.NewTicker(3 * time.Second) //设置定时器,定义timer的基本单位是三秒。
cnt := 0
for _ = range timer.C{ //其中timer.C就是一个channel
cnt++
if cnt > 3 {
timer.Stop() //调用方式
return //这里是手动停下来,一般是不会让他停下来,比如在监控的时候,但是有结束条件的时候用return的话是不会报错的
}
fmt.Println("yinzhengjie")
}
} #以上代码执行结果如下: yinzhengjie
yinzhengjie
yinzhengjie
B.time.After用法展示:
要注意的是,“time.NewTicker”需要调用"Stop"方法才会被执行,而“time.After”调用的姿势也不一样,我们一起来体验一下吧。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"time"
"fmt"
) func main() {
c := time.After(time.Second*3) //表示让其睡眠3秒钟。
<-c //调用方式
fmt.Println("主进程执行完毕")
} #以上代码执行结果如下:
主进程执行完毕
C.channel的深入浅出;
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"net/http"
"log"
"fmt"
"time"
) /*
1.channel就好像一个传送带,可以源源不断的往里面放数据,只要不close就可以永远发送数据。
2.如果channel里面没有数据,接收方会阻塞。
3.如果没有人正在等待channel的数据,发送方会阻塞。
4.从一个close的channel取数据永远不会阻塞,同时获取的是默认值
*/ func Print_url(url string) {
res,err := http.Get(url)
if err != nil {
log.Fatal("",err)
}
defer res.Body.Close()
fmt.Printf("网页的状态返回码是:%v\n",res.Status) } func work(resave chan string) { for {
url,ok := <- resave //如果channel里面没有数据,接收方会阻塞。用url,ok两个参数来接受channel,可以从channel会返回两个参数。url表示接受的数据。ok接受的信息是channel是否结束。
//fmt.Println(url,ok)
if !ok { //判断channel是否关闭。
break
}
Print_url(url)
} //for i := range resave { //range语法一值在接受数据,而channel可以源源不断的往里面添加数据。从一个close的channel取数据永远不会阻塞,同时获取的是默认值。
// Print_url(i)
//}
} func main() {
make_string := make(chan string)
for i := 0;i<3 ;i++ {
go work(make_string)
}
urls := []string{"http://www.baidu.com","http://www.qq.com","http://www.weixin.com"}
for _,i := range urls {
make_string <- i //如果没有人正在等待channel的数据,发送方会阻塞。
}
close(make_string) //channel就好像一个传送带,可以源源不断的往里面放数据,只要不close就可以永远发送数据。
time.Sleep(5 * time.Second)
} #以上代码输出结果如下:
网页的状态返回码是:200 OK
网页的状态返回码是:200 OK
网页的状态返回码是:200 OK
D.waitgroup+channel配合使用;
我们可以对C进行优化,在实际生产环境中,我们最好不要用"time.sleep"这个方法来实现主程序等待子程序执行完毕,因为会出现异响不到的bug哟~推荐是用waitgrout来取代这个sleep时间。
package main import (
"net/http"
"log"
"fmt"
//"time"
"sync"
) /*
1.channel就好像一个传送带,可以源源不断的往里面放数据,只要不close就可以永远发送数据。
2.如果channel里面没有数据,接收方会阻塞。
3.如果没有人正在等待channel的数据,发送方会阻塞。
4.从一个close的channel取数据永远不会阻塞,同时获取的是默认值
*/ func Print_url(url string) {
res,err := http.Get(url)
if err != nil {
log.Fatal("",err)
}
defer res.Body.Close()
fmt.Printf("网页的状态返回码是:%v\n",res.Status) } func work(resave chan string,wg *sync.WaitGroup) { for {
url,ok := <- resave //如果channel里面没有数据,接收方会阻塞。用url,ok两个参数来接受channel,可以从channel会返回两个参数。url表示接受的数据。ok接受的信息是channel是否结束。
//fmt.Println(url,ok)
if !ok { //判断channel是否关闭。
break
}
Print_url(url)
} //for i := range resave { //range语法一值在接受数据,而channel可以源源不断的往里面添加数据。从一个close的channel取数据永远不会阻塞,同时获取的是默认值。
// Print_url(i)
//}
wg.Done()
} func main() {
wg := new(sync.WaitGroup)
//var num = 3
//make_string := make(chan string)
//for i := 0;i<num ;i++ {
// go work(make_string,wg)
//}
//wg.Add(num) make_string := make(chan string)
for i := 0;i<3 ;i++ {
wg.Add(1) //启用的协程数和add的进程池保持一致。
go work(make_string,wg)
} urls := []string{"http://www.baidu.com","http://www.qq.com","http://www.weixin.com"}
for _,i := range urls {
make_string <- i //如果没有人正在等待channel的数据,发送方会阻塞。
}
close(make_string) //channel就好像一个传送带,可以源源不断的往里面放数据,只要不close就可以永远发送数据。
//time.Sleep(5 * time.Second)
wg.Wait() //等待
} #以上代码执行结果如下:
网页的状态返回码是:200 OK
网页的状态返回码是:200 OK
网页的状态返回码是:200 OK
七.爬虫进阶之路;
在我们想要写出来一个爬虫出来,我们首先得了解几个模块,知道它的用法,这样我们才能写出一个真正的爬虫,接下来跟我一起看看常用的几个模块吧。
1."net/http"模块用法展示;
"net/http"模块可用于爬去url的内容。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"net/http"
"log"
"io"
"os"
) func main() {
url := "http://www.xiaohuar.com" //定义一个URl,也就是我们要爬的网站。
resp,err := http.Get(url) //获取url的内容。
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close() //千万要关闭链接,不然会造成资源泄露。
if resp.StatusCode != http.StatusOK{ //如果返回状态出现错误,就抛出错误。
log.Fatal(resp.Status)
}
io.Copy(os.Stdout,resp.Body) //将正常的结果输出到屏幕上,并不会占用内存。
}
[root@yinzhengjie tmp]# more http.go
package main import (
"net/http"
"log"
"io"
"os"
) func main() {
url := "http://www.xiaohuar.com" //定义一个URl,也就是我们要爬的网站。
resp,err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close() //千万要关闭链接,不然会造成资源泄露。
if resp.StatusCode != http.StatusOK{ //如果返回状态出现错误,就抛出错误。
log.Fatal(resp.Status)
}
io.Copy(os.Stdout,resp.Body) //将结果输出到屏幕上,并不会占用内存。
} [root@yinzhengjie tmp]#
[root@yinzhengjie tmp]#
[root@yinzhengjie tmp]# go run http.go
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<meta http-equiv="Cache-Control" content="no-transform"/>
<meta http-equiv="Cache-Control" content="no-siteapp"/>
<link rel="alternate" media="only screen and(max-width: 640px)" href="http://m.xiaohuar.com/">
<title>-УУtitle><meta name="keywords" content="УУ <meta name="description" content="2015УУУƬУѧУУ">
<link rel="Shortcut Icon" type="image/x-icon" href="http://www.xiaohuar.com/favicon.ico"/>
<link type="text/css" rel="stylesheet" href="http://www.xiaohuar.com/skin/meizi/css/global.css"/>
<link type="text/css" rel="stylesheet" href="http://www.xiaohuar.com/skin/meizi/css/index2.css"/>
<link href="http://www.xiaohuar.com/skin/meizi/css/a.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://www.xiaohuar.com/skin/meizi/js/jquery.min.js"></script>
<script type="text/javascript" src="http://www.xiaohuar.com/skin/meizi/js/global.js"></script>
<script src="http://www.xiaohuar.com/skin/default/js/tophone.js" type="text/javascript"></script><script type="text/javascript">uaredirect("http://m.xiaohuar.com");</script>
<script type="text/javascript" src="http://www.xiaohuar.com/skin/meizi/js/focus.js"></script>
<script type="text/javascript" src="http://www.xiaohuar.com/skin/default/js/index.js"></script>
<script type="text/javascript" src="http://www.xiaohuar.com/skin/meizi/js/imglazyload.js"></script>
<meta property="qc:admins" content=""/>
</head>
<body>
<div class="g-hd"><div class="m-nav"><h1 class="m-logo"><a href="http://www.xiaohuar.com" title="У">У</a></h1><ul id="menu-nav" class="main-menu"><li id="menu-item-14599" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-14599"><a title="" href="http://www.xiaohuar.com/"></a></li><li id="menu-item-32780" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-32780"><a title="Уhref="http://www.xiaohuar.com/hua/">Уa></li><li id="menu-item-32781" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-32781"><a title="Уhref="http://www.xiaohuar.com/2014.html">Уa></li><li id="menu-item-32783" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-32783"><a title="Уhref="http://www.xiaohuar.com/xiaocao/">Уa></li><li id="menu-item-32782" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-32782"><a title="УƵ" href="http://www.xiaohuar.com/v/"></a></li><li id="menu-item-14595"><a title="У href="http://www.xiaohuar.com/mm/">Уa></li><li id="menu-item-14596" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-14596"><a title="Уhref="http://www.xiaohuar.com/nvshen/" target="_blank">Ůa></li></ul><div class="searchform"><script type="text/javascript">document.write(unescape('%3Cdiv id="bdcs"%3E%3C/div%3E%3Cscript charset="utf-8" src="http://znsv.baidu.com/customer_search/api/js?sid=16660441808226271668') + '&plate_url=' + (encodeURIComponent(window.location.href)) + '&t=' + (Math.ceil(new Date()/3600000)) + unescape('"%3E%3C/script%3E'));</script></div></div></div>
<div style="margin-top:80px;">
</div><div class="jiaodian"> <div id="inner">
<div class="hot-event">
<div class="switch-nav"><a href="#" onclick="return false;" class="prev"><i class="ico i-prev"></i><span class="hide-clip">span><span class="blackbg"></span></a><a href="#" onclick="return false;" class="next"><i class="ico i-next"></i><span class="hide-clip">span><span class="blackbg"></span></a></div>
<div class="event-item" style="display: block;"><a href="http://www.xiaohuar.com/html/zhongxue/" target="_blank" class="banner"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/images/banner/2.jpg" class="photo" style="width: 1000px; height: 400px;" alt="Ůϲ˽></a></div><div class="event-item" style="display: none;"><a href="http://www.xiaohuar.com/html/dasai/" target="_blank" class="banner"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/images/banner/3.jpg" class="photo" style="width: 1000px; height: 400px;" alt="д></a></div><div class="event-item" style="display: none;"><a href="http://www.xiaohuar.com/p-6-27.html" target="_blank" class="banner"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/images/banner/4.jpg" class="photo" style="width: 1000px; height: 400px;" alt="ɭ></a></div><div class="event-item" style="display: none;"><a href="http://www.xiaohuar.com/p-6-26.html" target="_blank" class="banner"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/images/banner/5.jpg" class="photo" style="width: 1000px; height: 400px;" alt=""/></a></div><div class="event-item" style="display: none;"><a href="http://www.xiaohuar.com/p-6-25.html" target="_blank" class="banner"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/images/banner/6.jpg" class="photo" style="width: 1000px; height: 400px;" alt="ʷ欶/></a></div>
<div class="switch-tab">
<a href="#" onclick="return false;" hidefocus="true" class="current">1</a>
<a href="#" onclick="return false;" hidefocus="true">2</a>
<a href="#" onclick="return false;" hidefocus="true">3</a>
<a href="#" onclick="return false;" hidefocus="true">4</a>
<a href="#" onclick="return false;" hidefocus="true">5</a>
</div>
</div>
</div>
<script type="text/javascript">
$('#inner').nav({ t: 2000, a: 1000 });
</script> </div>
<div class="all_1000">
<div class="jingxuan ">
<div class="title1000"><div class="jingxuan "><div class="title1000"><div class="title jx"><a>ͼƬ</a></div>
<div class="titleleft"><a href="http://www.xiaohuar.com/news/" target="_blank">Уa>|<a href="http://www.xiaohuar.com/meinv/" target="_blank"></a>|<a href="http://www.xiaohuar.com/2014.html" target="_blank">У</a>|<a href="http://www.xiaohuar.com/daxue/" target="_blank">Уa>|<a href="http://www.xiaohuar.com/hot/" target="_blank"></a></div><div style="float:right;padding-top:10px;"><div class="bdsharebuttonbox"><a href="#" class="bds_more" data-cmd="more"></a><a href="#" class="bds_qzone" data-cmd="qzone" title=""></a><a href="#" class="bds_tsina" data-cmd="tsina" title="</a><a href="#" class="bds_tqq" data-cmd="tqq" title="Ѷ</a><a href="#" class="bds_weixin" data-cmd="weixin" title="</a><a href="#" class="bds_sqq" data-cmd="sqq" title=""></a><a href="#" class="bds_tieba" data-cmd="tieba" title="</a></div>
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"","bdMiniList":false,"bdPic":"","bdStyle":"","bdSize":""},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script></div></div>
</div>
</div>
<div class="img1000">
<ul><li><a href="http://www.xiaohuar.com/hua/" target="_blank" title="У<i></i><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/images/banner/a.jpg" alt="У></a></li><li><a href="http://www.xiaohuar.com/xiaocao/" target="_blank" title="У<i></i><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/images/banner/b.jpg" alt="У></a></li><li><a href="http://www.xiaohuar.com/mm/" target="_blank" title="У<i></i><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/images/banner/c.jpg" alt="У></a></li><li><a href="http://www.xiaohuar.com/meinv/" target="_blank" title=""><i></i><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/images/banner/d.jpg" alt=""/></a></li></ul>
</div>
</div>
<div class="clear"></div><div class="adv100090" style="margin-top: 15px;text-align: center;padding: 20px 0px;background-color: #f5f5f5;"> <script type="text/javascript">
/*banner*/
var cpro_id = "u2170109";
</script>
<script type="text/javascript" src="http://cpro.baidustatic.com/cpro/ui/c.js"></script>
</div>
<div class="all_lanmu ">
<div class="title1000"><div class="title xg"><a>Уa></div><div class="more"></div>
</div>
<ul class="twoline">
<li><a href="http://www.xiaohuar.com/p-1-1933.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170628/f3d06ef49965aedbe18286a2f221fd9f.jpg"/></a><a href="http://www.xiaohuar.com/p-1-1933.html" target="_blank"><span>ҵѧԺУspan></a><b class="b1">ҵѧԺ</b><b class="b2">81</b></li>
<li><a href="http://www.xiaohuar.com/p-1-1932.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170626/0ab1d89f54c90df477a90aa533ceea36.jpg"/></a><a href="http://www.xiaohuar.com/p-1-1932.html" target="_blank"><span>ְҵѧԺУ</span></a><b class="b1">ְҵѧԺ</b><b class="b2">4</b></li>
<li><a href="http://www.xiaohuar.com/p-1-1931.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170619/e0456729d4dcbea569a1acbc6a47ab69.jpg"/></a><a href="http://www.xiaohuar.com/p-1-1931.html" target="_blank"><span>ְҵѧԺУ</span></a><b class="b1">ְҵѧԺ</b><b class="b2">21</b></li>
<li><a href="http://www.xiaohuar.com/p-1-1928.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170612/1f6620771e2815d21f37d481fa8311e6.png"/></a><a href="http://www.xiaohuar.com/p-1-1928.html" target="_blank"><span>ɽУ</span></a><b class="b1">ɽ</b><b class="b2">32</b></li>
<li><a href="http://www.xiaohuar.com/p-1-1926.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170604/ec3794d0d42b538bf4461a84dac32509.jpg"/></a><a href="http://www.xiaohuar.com/p-1-1926.html" target="_blank"><span>ʵ</span></a><b class="b1">ʵb><b class="b2">36</b></li>
<li><a href="/p-1-338.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150313150700127.jpg"/></a><a href="/p-1-338.html" target="_blank"><span>ѧԺУѩcc</span></a><b class="b1">ѧԺ</b><b class="b2">106</b></li>
<li><a href="/p-1-165.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150306205302174.jpg"/></a><a href="/p-1-165.html" target="_blank"><span>ѧԺУ</span></a><b class="b1">ѧԺ</b><b class="b2">78</b></li>
<li><a href="http://www.xiaohuar.com/p-1-667.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150716115704192.jpg"/></a><a href="http://www.xiaohuar.com/p-1-667.html" target="_blank"><span>ѧԺУspan></a><b class="b1">ѧԺ</b><b class="b2">150</b></li>
<li><a href="/p-1-390.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150313162704133.jpg"/></a><a href="/p-1-390.html" target="_blank"><span>У</span></a><b class="b1"></b><b class="b2">351</b></li>
<li><a href="/p-1-363.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/aaa5491c03fce0c6c3806c838fd64980.jpg"/></a><a href="/p-1-363.html" target="_blank"><span>У</span></a><b class="b1"></b><b class="b2">220</b></li>
</ul>
</div>
<div class="all_1000">
<div class="title1000"><div class="title mx"><a href="http://www.xiaohuar.com/news/" target="_blank">Уa></div><div class="more"></div>
</div>
<div class="mx_left">
<div class="mx240">
<div class="img240 piclunbo">
<a href="http://www.xiaohuar.com/hot/188.html" class='on' target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/news/20160328/1459134125894888.jpg" width="" height="" alt="ǫע<em></em><span>ǫעpan></a><a href="http://www.xiaohuar.com/p-5-148.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/news/20151027/1445918569133368.jpg" width="240" height="150" alt="ѧԺУ><em></em><span>ѧԺУspan></a>
<a href="http://www.xiaohuar.com/p-5-145.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/news/20151026/1445832517110699.jpg" width="" height="" alt="<em></em><span>pan></a>
</div>
<ul> <li class='kuang'><a href="javascript:;"><i></i><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/news/20160328/1459134125894888.jpg" width="" height="" alt="ǫע</a></li> <li><a href="javascript:;"><i></i><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/news/20151027/1445918569133368.jpg" width="72" height="50" alt="ѧԺУ></a></li>
<li><a href="javascript:;"><i></i><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/news/20151026/1445832517110699.jpg" width="" height="" alt="</a></li>
</ul> </div>
<div class="mx240_list">
<div class="title240"><a href="http://www.xiaohuar.com/news/" target="_blank"></a></div>
<ul>
<li><a href="http://www.xiaohuar.com/news-1-1933.html" target="_blank" title="">ҵѧԺУa></li> <li><a href="http://www.xiaohuar.com/news-1-1932.html" target="_blank" title="ְҵѧԺУ</a></li> <li><a href="http://www.xiaohuar.com/news-1-1931.html" target="_blank" title="֣">ְҵѧԺУ</a></li> <li><a href="http://www.xiaohuar.com/news-1-1928.html" target="_blank" title="ɽУ</a></li> <li><a href="http://www.xiaohuar.com/hot/188.html" target="_blank" title="ǫע</a></li> <li><a href="http://www.xiaohuar.com/hot/183.html" target="_blank" title="ϲ">ϲ</a></li> <li><a href="http://www.xiaohuar.com/hot/182.html" target="_blank" title="MM">MM</a></li> <li><a href="http://www.xiaohuar.com/hot/179.html" target="_blank" title="ŮChoi Somi)ʾ">ŮChoi Somi)ʾ</a></li> <li><a href="http://www.xiaohuar.com/hot/168.html" target="_blank" title="ô">ô</a></li> </ul>
</div>
</div>
<div class="mx_con">
<div class="mx_con_top">
<div class="div1"><a href="http://www.xiaohuar.com/p-7-10.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" width="" lazysrc="/d/file/20150827/f341b4ac6aaadd8d935c9de04cc5d812.jpg"/><span>jolie</span><em></em></a></div>
<div class="div2"><div class="img125120">
<a href="http://www.xiaohuar.com/p-1-1548.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" width="" lazysrc="/d/file/20160412/434d0f660ad823fa3d0b12b115a6af99.jpg"/><span>ӯ</span><em></em></a> </div><div class="clearh10"></div><div class="img125170"> <a href="/p-1-328.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20141204065514172.jpg" width=""/><span></span><em></em></a> </div></div>
<div class="div2"><div class="img125170"><a href="http://www.xiaohuar.com/p-1-1854.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" width="" lazysrc="/d/file/20170227/06eb36fb8d8708551b7a53f8e07e77a7.jpg"/><span>span><em></em></a> </div><div class="clearh10"></div><div class="img125120"><a href="/p-1-126.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/c34082612850c3a01922c960c2384bbf.jpg" width=""/><span>span><em></em></a> </div></div>
</div>
<div class="mx_con_down">
<div class="title470">Ůdiv><div class="img155200"><a href="http://www.xiaohuar.com/p-6-3.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150815123444171.jpg" alt="Yurisa"/><span>Yurisa-</span><em></em></a></div>
<div class="ulbox">
<ul><li><a href="http://www.xiaohuar.com/p-5-48.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/news/20150815/1439615307134745.jpg" alt="><span>span><em></em></a></li><li><a href="http://www.xiaohuar.com/p-5-49.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/news/20150815/1439615732117201.jpg" alt="><span>span><em></em></a></li><li><a href="http://www.xiaohuar.com/p-5-47.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/news/20150815/1439615048116633.jpg" alt="><span>span><em></em></a></li><li><a href="http://www.xiaohuar.com/p-1-681.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/news/20150815/1439616247184348.jpg" alt="><span>span><em></em></a></li><li><a href="http://www.xiaohuar.com/p-1-749.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://img.1985t.com/uploads/previews/2014/12/0-rcAIKA.jpg" alt="><span>span><em></em></a></li><li><a href="http://www.xiaohuar.com/p-1-136.html" target="_blank"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://img.1985t.com/uploads/previews/2014/12/0-ywgsac.jpg" alt="><span>span><em></em></a></li>
<div class="clear"></div>
</ul>
</div>
</div>
</div>
<div class="mx_right">
<div class="mx_alaal"><div class="title240">Уѯ</div><div class="mx_search">
<form method="get" target="_self" action="/star/section">
<div class="search_ipt"><input name="keywords" class="s_ipt" id="searchstar" autocomplete="off" type="text" value="" onfocus="if(this.value==''){this.value=''}; this.style.color='#666'" onblur="if(this.value==''){this.value=''}; this.style.color='#C0C0C0'" style="color:#C0C0C0;"/>
<input type="hidden" id="filed_quyu" name="quyu" value=""/>
<input type="hidden" id="filed_fenlei" name="fenlei" value=""/>
<input class="serch_btn" type="submit" value="">
</div>
</form>
</div>
<div class="taglist_outer"><dl class="taglist"><dt>/dt><dd id="div_genre" data='quyu'><a href="#" data=""></a><a href="#" data=""></a><a href="#" data="">Ůa><a href="#" data=""></a></dd></dl>
<div class="tagsearch">Уa href="http://www.xiaohuar.com/p-1-3.html" target="_blank">MM</a><a href="http://www.xiaohuar.com/p-1-563.html" target="_blank">С</a><a href="http://www.xiaohuar.com/daxue/xiamen/" target="_blank"></a><a href="http://www.xiaohuar.com/p-1-95.html" target="_blank">a><a href="http://www.xiaohuar.com/p-1-136.html" target="_blank">a><a href="http://www.xiaohuar.com/p-1-169.html" target="_blank">a><a href="http://www.xiaohuar.com/p-1-290.html" target="_blank">a><a href="http://www.xiaohuar.com/p-1-5.html" target="_blank"></a></div>
</div>
</div>
<div class="big_lunhuan"> <div class="mod-hd"><div class="mod-hd-title"></div></div> <style>.mypicchange ul{border-top:1px solid #eaeaea;}.mypicchange ul li{border:1px solid #eaeaea;border-top:0px;height:40px;line-height:40px;}.mypicchange ul li .yuletit{padding:0px 10px;height:40px;line-height:40px;overflow:hidden;}.mypicchange ul li .yulepic{display:none;}.mypicchange ul li.on{height:auto;}.mypicchange ul li.on .yuletit{display:none;}.mypicchange ul li.on .yulepic{display:block;}.mypicchange ul li.on .yulepic img{width:238px;}.mypicchange ul li .yulepic h3{padding:0px 10px;font-size:14px;height:40px;line-height:40px;overflow:hidden;text-align:center;background-color:#f7f7f7;}.mypicchange ul li .yulepic h3 a{color:#c39;display:block;}</style>
<script>
$(function(){
$('.mypicchange ul li').bind('mouseover',function(){
$('.mypicchange ul li').removeClass('on');
$(this).addClass('on');
});
});
</script>
<div class="accordion-news-side">
<div class="mypicchange">
<ul><li class='on'><div class="yuletit"><a href="http://www.xiaohuar.com/p-5-50.html">a></div><div class="yulepic"><h3><a href="http://www.xiaohuar.com/p-5-50.html">a></h3><p><a href="http://www.xiaohuar.com/p-5-50.html" title="<img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/news/20150815/1439617608990548.jpg" width="238" alt="</a></p></div></li><li><div class="yuletit"><a href="http://www.xiaohuar.com/p-1-310.html">У</a></div><div class="yulepic"><h3><a href="http://www.xiaohuar.com/p-1-310.html">У</a></h3><p><a href="http://www.xiaohuar.com/p-1-310.html" title="У"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/news/20150815/1439618030131259.jpg" width="" alt="У"></a></p></div></li><li><div class="yuletit"><a href="http://www.xiaohuar.com/p-1-64.html">У</a></div><div class="yulepic"><h3><a href="http://www.xiaohuar.com/p-1-64.html">У</a></h3><p><a href="http://www.xiaohuar.com/p-1-64.html" title="У"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/news/20150815/1439618379649349.jpg" width="" alt="У"></a></p></div></li><li><div class="yuletit"><a href="http://www.xiaohuar.com/p-1-752.html">Ůͼ бҲ</a></div><div class="yulepic"><h3><a href="http://www.xiaohuar.com/p-1-752.html">Ůͼ бҲ</a></h3><p><a href="http://www.xiaohuar.com/p-1-752.html" title="Ůͼ бҲ"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150815141329100.jpg" width="" alt="Ůͼ бҲ"></a></p></div></li><li><div class="yuletit"><a href="http://www.xiaohuar.com/p-1-383.html">Уa></div><div class="yulepic"><h3><a href="http://www.xiaohuar.com/p-1-383.html">Уa></h3><p><a href="http://www.xiaohuar.com/p-1-383.html" title="У<img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150815141814102.jpg" width="238" alt="У</a></p></div></li></ul>
</div>
</div> </div>
</div>
</div>
<div class="all_lanmu ">
<div class="title1000"><div class="title mytitle myw_sexy"><a href="/mm/" target="_blank">Уa></div><div class="more"><a href="/mm/" target="_blank">MORE+</a></div></div>
<ul class="twoline">
<li><a href="http://www.xiaohuar.com/p-4-639.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150221175512120.jpg"/></a><a href="http://www.xiaohuar.com/p-4-639.html" target="_blank"><span>ʯʨѧУ</span></a><b class="b1">ʯʨѧ</b><b class="b2">91</b></li>
<li><a href="http://www.xiaohuar.com/p-4-706.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/d33b2cae4fdfa7110fa8049db325838f.jpg"/></a><a href="http://www.xiaohuar.com/p-4-706.html" target="_blank"><span>loly~~~~~~~У~~~~</span></a><b class="b1">loly~~~~~~~</b><b class="b2">69</b></li>
<li><a href="http://www.xiaohuar.com/p-4-486.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150414092659161.jpg"/></a><a href="http://www.xiaohuar.com/p-4-486.html" target="_blank"><span>УֱгŴ˽span></a><b class="b1"></b><b class="b2">75</b></li>
<li><a href="http://www.xiaohuar.com/p-4-691.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/9bfe870df5ea859c219b6f2733e513b1.jpg"/></a><a href="http://www.xiaohuar.com/p-4-691.html" target="_blank"><span>У</span></a><b class="b1"></b><b class="b2">85</b></li>
<li><a href="http://www.xiaohuar.com/p-4-1122.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20160109/7f860f71d979a57531132f91677376bb.jpg"/></a><a href="http://www.xiaohuar.com/p-4-1122.html" target="_blank"><span>Уspan></a><b class="b1"></b><b class="b2">84</b></li>
<li><a href="http://www.xiaohuar.com/p-4-867.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20151003/11f8c32b40eab5269d18e68d51185d8d.jpg"/></a><a href="http://www.xiaohuar.com/p-4-867.html" target="_blank"><span></span></a><b class="b1">b><b class="b2">64</b></li>
<li><a href="http://www.xiaohuar.com/p-4-1099.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20151231/ddb79b6dcdbc876059cd6db9730d7656.jpg"/></a><a href="http://www.xiaohuar.com/p-4-1099.html" target="_blank"><span>DZɽҰկУ</span></a><b class="b1">DZɽҰկ</b><b class="b2">48</b></li>
<li><a href="http://www.xiaohuar.com/p-4-797.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20150902/890c8a533c0346c19bb84ff90a36e4fd.jpg"/></a><a href="http://www.xiaohuar.com/p-4-797.html" target="_blank"><span>У</span></a><b class="b1"></b><b class="b2">58</b></li>
<li><a href="http://www.xiaohuar.com/p-4-922.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20151103/d89e18e42269f9169dfcffba836f2795.jpg"/></a><a href="http://www.xiaohuar.com/p-4-922.html" target="_blank"><span>ְҵѧԺУ</span></a><b class="b1">ְҵѧԺ</b><b class="b2">58</b></li>
<li><a href="http://www.xiaohuar.com/p-4-1114.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20160107/776911c54ae5ed4f0fe1c3652ce755da.jpg"/></a><a href="http://www.xiaohuar.com/p-4-1114.html" target="_blank"><span>У</span></a><b class="b1"></b><b class="b2">188</b></li>
</ul>
</div>
<div class="all_lanmu ">
<div class="title1000"><div class="title mytitle myw_fresh"><a href="/xiaocao/" target="_blank">Уa></div><div class="more"><a href="/xiaocao/" target="_blank">MORE+</a></div></div>
<ul class="twoline">
<li><a href="http://www.xiaohuar.com/p-2-1930.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170613/814725f45667309a6066e93b1ad2e378.jpg"/></a><a href="http://www.xiaohuar.com/p-2-1930.html" target="_blank"><span>У</span></a><b class="b1"></b><b class="b2">1</b></li>
<li><a href="http://www.xiaohuar.com/p-2-1927.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170608/f07501a9d1fe9945e2fb96f0890a8d23.jpg"/></a><a href="http://www.xiaohuar.com/p-2-1927.html" target="_blank"><span>ʦѧУspan></a><b class="b1">ʦѧ</b><b class="b2">1600</b></li>
<li><a href="http://www.xiaohuar.com/p-2-1915.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170601/b5ceb0d78d4e2d15f5bf3e9085fdc638.jpg"/></a><a href="http://www.xiaohuar.com/p-2-1915.html" target="_blank"><span>У</span></a><b class="b1"></b><b class="b2">1</b></li>
<li><a href="http://www.xiaohuar.com/p-2-1907.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170528/68f95c381d71baeb3290cf686f75cf6e.jpg"/></a><a href="http://www.xiaohuar.com/p-2-1907.html" target="_blank"><span>ѧУ˼</span></a><b class="b1">ѧ</b><b class="b2">1</b></li>
<li><a href="http://www.xiaohuar.com/p-2-1900.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170505/6de1dfa7b06149a8e4f58d4531f1e0a6.jpg"/></a><a href="http://www.xiaohuar.com/p-2-1900.html" target="_blank"><span>һСУ</span></a><b class="b1">һС</b><b class="b2">1</b></li>
<li><a href="http://www.xiaohuar.com/p-2-1899.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170504/b44a79761757d24941c3f1205630f265.jpg"/></a><a href="http://www.xiaohuar.com/p-2-1899.html" target="_blank"><span>ѧԺУkey</span></a><b class="b1">ѧԺ</b><b class="b2">3</b></li>
<li><a href="http://www.xiaohuar.com/p-2-1880.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170416/55e0a68c6d7b985dca286f24b4a0b4ed.jpg"/></a><a href="http://www.xiaohuar.com/p-2-1880.html" target="_blank"><span>У</span></a><b class="b1"></b><b class="b2">2</b></li>
<li><a href="http://www.xiaohuar.com/p-2-1877.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170413/31572d773f2117c4dd3c1a4552fb64c9.jpg"/></a><a href="http://www.xiaohuar.com/p-2-1877.html" target="_blank"><span>ѧУУԣspan></a><b class="b1">ѧУ</b><b class="b2">2</b></li>
<li><a href="http://www.xiaohuar.com/p-2-1876.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170413/aaeaa82b8eccb31323153201f0d35c69.jpg"/></a><a href="http://www.xiaohuar.com/p-2-1876.html" target="_blank"><span>ѧԺУspan></a><b class="b1">ѧԺ</b><b class="b2">3</b></li>
<li><a href="http://www.xiaohuar.com/p-2-1868.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20170404/e1bc3ee2190f02b13a4b65fa78839df8.jpg"/></a><a href="http://www.xiaohuar.com/p-2-1868.html" target="_blank"><span>У.У</span></a><b class="b1">У.</b><b class="b2">3</b></li>
</ul>
</div>
<div class="all_lanmu ">
<div class="title1000"><div class="title mytitle myw_beauty"><a href="/meinv/" target="_blank"></a></div><div class="more"><a href="/meinv/" target="_blank">MORE+</a></div></div>
<ul class="twoline">
<li><a href="http://www.xiaohuar.com/p-6-36.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20151112/6c4f48884832ea2700695b2849628ac5.jpg"/></a><a href="http://www.xiaohuar.com/p-6-36.html" target="_blank"><span>ԡ</span></a><b class="b1">2015-11-12</b><b class="b2">7445</b></li>
<li><a href="http://www.xiaohuar.com/p-6-4.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150503184242161.jpg"/></a><a href="http://www.xiaohuar.com/p-6-4.html" target="_blank"><span>90rbieͯҡ</span></a><b class="b1">2015-05-03</b><b class="b2">4499</b></li>
<li><a href="http://www.xiaohuar.com/p-6-26.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150814223831181.jpg"/></a><a href="http://www.xiaohuar.com/p-6-26.html" target="_blank"><span></span></a><b class="b1">2015-08-14</b><b class="b2">10647</b></li>
<li><a href="http://www.xiaohuar.com/p-6-44.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20160408/40e4e29b7156e316d4f877d8d0906ff7.jpg"/></a><a href="http://www.xiaohuar.com/p-6-44.html" target="_blank"><span>Ϫдspan></a><b class="b1">2016-04-08</b><b class="b2">4635</b></li>
<li><a href="http://www.xiaohuar.com/p-6-12.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150630151831172.jpg"/></a><a href="http://www.xiaohuar.com/p-6-12.html" target="_blank"><span>̨ƷShowGirlҦ span></a><b class="b1">2015-06-30</b><b class="b2">5374</b></li>
<li><a href="http://www.xiaohuar.com/p-6-24.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150713212708135.jpg"/></a><a href="http://www.xiaohuar.com/p-6-24.html" target="_blank"><span>MiuMiu </span></a><b class="b1">2015-07-13</b><b class="b2">5493</b></li>
<li><a href="http://www.xiaohuar.com/p-6-43.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20160408/8434145de5a9bcd6f06e06a780b8a8b3.jpg"/></a><a href="http://www.xiaohuar.com/p-6-43.html" target="_blank"><span></span></a><b class="b1">2016-04-08</b><b class="b2">2137</b></li>
<li><a href="http://www.xiaohuar.com/p-6-17.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150702184343113.jpg"/></a><a href="http://www.xiaohuar.com/p-6-17.html" target="_blank"><span>34E~ʯùpan></a><b class="b1">2015-07-02</b><b class="b2">2935</b></li>
<li><a href="http://www.xiaohuar.com/p-6-11.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="http://www.xiaohuar.com/d/file/20150602175624110.jpg"/></a><a href="http://www.xiaohuar.com/p-6-11.html" target="_blank"><span>һ˿COSspan></a><b class="b1">2015-06-02</b><b class="b2">3419</b></li>
<li><a href="http://www.xiaohuar.com/p-6-42.html" target="_blank" class="xhpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20160408/bf5819982372761ea8c2bcade410fb9b.jpg"/></a><a href="http://www.xiaohuar.com/p-6-42.html" target="_blank"><span>ģѩдspan></a><b class="b1">2016-04-08</b><b class="b2">2587</b></li>
</ul>
</div>
<div class="all_lanmu ">
<div class="title1000"><div class="title mytitle myw_model"><a href="/v/" target="_blank">УƵ</a></div><div class="more"><a href="/v/" target="_blank">MORE+</a></div></div>
<ul class="oneline" style="height: 230px;"><li><a href="http://www.xiaohuar.com/p-3-97.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20161208/878a9b4f94d401a8c2651f2b04f8c94e.jpg"/></a><a href="http://www.xiaohuar.com/p-3-97.html" target="_blank"><span>,СŶ</span></a><b class="b1">2016-12-08</b><b class="b2">9356</b></li>
<li><a href="http://www.xiaohuar.com/p-3-96.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20161113/adbdd1dd3924922398f2f6efc31ffee7.jpg"/></a><a href="http://www.xiaohuar.com/p-3-96.html" target="_blank"><span>ҡƵ</span></a><b class="b1">2016-11-13</b><b class="b2">6575</b></li>
<li><a href="http://www.xiaohuar.com/p-3-95.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20161103/c7b58bfaf538f2ede89a4f60b1ecc29b.jpg"/></a><a href="http://www.xiaohuar.com/p-3-95.html" target="_blank"><span>ֱ˿</span></a><b class="b1">2016-11-03</b><b class="b2">10782</b></li>
<li><a href="http://www.xiaohuar.com/p-3-94.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20161102/68e926a6a685b1ee0a012e89d626397a.jpg"/></a><a href="http://www.xiaohuar.com/p-3-94.html" target="_blank"><span></span></a><b class="b1">2016-11-02</b><b class="b2">5220</b></li>
<li><a href="http://www.xiaohuar.com/p-3-93.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20161102/4bb76725c4d4ac5077b9a1c594c5cbb2.jpg"/></a><a href="http://www.xiaohuar.com/p-3-93.html" target="_blank"><span>Ƶ</span></a><b class="b1">2016-11-02</b><b class="b2">14255</b></li>
<li><a href="http://www.xiaohuar.com/p-3-92.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20161027/f070412c9571ee1e5cbde31a816f3bad.jpg"/></a><a href="http://www.xiaohuar.com/p-3-92.html" target="_blank"><span>˿span></a><b class="b1">2016-10-27</b><b class="b2">7523</b></li>
<li><a href="http://www.xiaohuar.com/p-3-91.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20161027/185dc525e53fcd53cc30dd9e94f41114.jpg"/></a><a href="http://www.xiaohuar.com/p-3-91.html" target="_blank"><span></span></a><b class="b1">2016-10-27</b><b class="b2">8720</b></li>
<li><a href="http://www.xiaohuar.com/p-3-90.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20161016/e846aa103fe104b54b4e8a9ede378b9d.jpg"/></a><a href="http://www.xiaohuar.com/p-3-90.html" target="_blank"><span>ԡspan></a><b class="b1">2016-10-16</b><b class="b2">8271</b></li>
<li><a href="http://www.xiaohuar.com/p-3-89.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20161016/64a7e1893ccbc07a302a176480df3e79.jpg"/></a><a href="http://www.xiaohuar.com/p-3-89.html" target="_blank"><span>ɬ ͬһŮ</span></a><b class="b1">2016-10-16</b><b class="b2">8097</b></li>
<li><a href="http://www.xiaohuar.com/p-3-88.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20160619/5c3ede0e1d50f38adbfe8c34afd46771.jpg"/></a><a href="http://www.xiaohuar.com/p-3-88.html" target="_blank"><span></span></a><b class="b1">2016-06-19</b><b class="b2">14526</b></li>
</ul>
</div>
<div class="all_lanmu ">
<div class="title1000"><div class="title mytitle myw_battoo"><a href="/news/" target="_blank">У̬</a></div><div class="more"><a href="/news/" target="_blank">MORE+</a></div></div>
<ul class="oneline" style="height: 230px;"><li><a href="http://www.xiaohuar.com/p-5-25.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/bac1f0d8253b4e157fbf40b8a3edab56.jpg"/></a><a href="http://www.xiaohuar.com/p-5-25.html" target="_blank"><span>У/span></a><b class="b1">2015-07-16</b><b class="b2">2147</b></li>
<li><a href="http://www.xiaohuar.com/p-5-50.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/a0bbfc787c3179a029155871063cee0f.jpg"/></a><a href="http://www.xiaohuar.com/p-5-50.html" target="_blank"><span>span></a><b class="b1">2015-08-15</b><b class="b2">3856</b></li>
<li><a href="http://www.xiaohuar.com/p-5-33.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/news/20150807/1438960820583080.jpg"/></a><a href="http://www.xiaohuar.com/p-5-33.html" target="_blank"><span>̨ ̨</span></a><b class="b1">2015-08-07</b><b class="b2">2874</b></li>
<li><a href="http://www.xiaohuar.com/p-5-51.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/9520c71801cd4896ff3c7346b542b84d.jpg"/></a><a href="http://www.xiaohuar.com/p-5-51.html" target="_blank"><span>У</span></a><b class="b1">2015-08-15</b><b class="b2">2430</b></li>
<li><a href="http://www.xiaohuar.com/p-5-71.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20150830/576cf065a7d34716f0a8c3b27c546098.jpg"/></a><a href="http://www.xiaohuar.com/p-5-71.html" target="_blank"><span>רԺУspan></a><b class="b1">2015-08-30</b><b class="b2">3263</b></li>
<li><a href="http://www.xiaohuar.com/p-5-105.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/news/20151017/1445074354107162.jpg"/></a><a href="http://www.xiaohuar.com/p-5-105.html" target="_blank"><span>ѧԺУ˭</span></a><b class="b1">2015-10-17</b><b class="b2">1639</b></li>
<li><a href="http://www.xiaohuar.com/p-5-54.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/news/20150817/1439782814996443.jpg"/></a><a href="http://www.xiaohuar.com/p-5-54.html" target="_blank"><span>Ƭ</span></a><b class="b1">2015-08-17</b><b class="b2">2582</b></li>
<li><a href="http://www.xiaohuar.com/p-5-39.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/news/20150812/1439349313115531.jpg"/></a><a href="http://www.xiaohuar.com/p-5-39.html" target="_blank"><span>¶ǰspan></a><b class="b1">2015-08-12</b><b class="b2">1251</b></li>
<li><a href="http://www.xiaohuar.com/p-5-69.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/20150828/533b346740f18ad15551a3983f3ec333.jpg"/></a><a href="http://www.xiaohuar.com/p-5-69.html" target="_blank"><span>УУԪspan></a><b class="b1">2015-08-28</b><b class="b2">1465</b></li>
<li><a href="http://www.xiaohuar.com/p-5-145.html" target="_blank" class="vpic"><img src="http://www.xiaohuar.com/skin/meizi/images/grey.gif" lazysrc="/d/file/news/20151026/1445832517110699.jpg"/></a><a href="http://www.xiaohuar.com/p-5-145.html" target="_blank"><span>pan></a><b class="b1">2015-10-26</b><b class="b2">2540</b></li>
</ul>
</div>
<div class="friendlinks"><div class="linkbt"><h2></h2></div><p> <a href="http://www.dxsabc.com/" target="_blank"></a> <a href="http://www.7kk.com/" target="_blank">ͼƬ</a>
<a href="http://hufu.onlylady.com/" target="_blank"></a> <a href="http://www.cooltuku.com" target="_blank"></a> <a href="http://www.4j4j.cn" target="_blank"></a> <a href="http://www.omtxw.com" target="_blank">ŷ</a><a href="http://www.17sucai.com" target="_blank">a> <a href="http://www.iqingren.com" target="_blank">a> <a href="http://www.zhw123.cc">ŮͼƬ</a> <a href="http://www.xiaohuar.com/" target="_blank">Уa><a href="http://jiaoyu.baidu.com/beauty/index/" rel="nofollow" target="_blank">ѧa> <a href="http://www.sumiaozhijia.com/" target="_blank"></a> <a href="http://www.zeihaoxiao.com/" target="_blank">Ц</a> <a href="http://www.showself.com" target="_blank"></a> <a href="http://www.xiannvw.com" target="_blank"></a>
</p>
</div>
</div>
<div class="indexfooter">
<div class="foot"><div class="f_about"><ul><li><a href="http://www.xiaohuar.com/html/about.html" target="_blank"></a></li><li><a href="http://www.xiaohuar.com/html/about.html#content2" target="_blank"></a></li><li class="foot4"></li><li><a href="/about.html#content3" target="_blank"></a></li><li><a href="http://www.xiaohuar.com/html/about.html#content4" target="_blank"></a></li></ul></div>
<p><a href="http://www.xiaohuar.com/">У</a> href="http://www.xiaohuar.com/">ȫѧУƬ</a>ɢͬУͼƬ href="http://www.xiaohuar.com/">УƬ</a>ԭУƬr/><a href="http://www.xiaohuar.com/html/sitemap.htm">Уa> © 2009-2015 All Rights Reserved www.xiaohuar.com class="__cf_email__" href="/cdn-cgi/l/email-protection" data-cfemail="c0f2f1f1f2f4f9f2f3f680b1b1eea3afad">[email protected]</a><script data-cfhash='f9e31' type="text/javascript">/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */</script> P006982 <script language="javascript" type="text/javascript" src="http://js.users.51.la/17172513.js"></script></p>
</div>
</div>
<div id="back-to-top" class="back-to-top"><a target="_self" href="javascript:void(0);" id="back-to-top-btn"> </a></div>
<script type="text/javascript" src="http://www.xiaohuar.com/skin/meizi/js/IE6Top.js"></script>
<div id="cbbfixed" style="bottom: 366px;_bottom: 0px;">
<div class="dingyue">
<a href="javascript:;"></a>
<div class="qqdingyue"> <script>var nId = "b8f6592311f7b82244e30894c7c84632de2c2675bb9329e6",nWidth="auto",sColor="light",sText=";</script>
<script src="http://list.qq.com/zh_CN/htmledition/js/qf/page/qfcode.js" charset="gb18030"></script>
</div>
</div>
<a id="cweixin" href="#"><span></span><div></div></a>
<div class="jianyi"><a href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=aFpZWVpcUVpbXigZGUYLBwU" target="_blank"></a></div></div>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?0dfa94cc970f5368ddbe743609970944";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script><script type="text/javascript">
/**/
var cpro_id = "u1960876";
</script>
<script type="text/javascript" src="http://cpro.baidustatic.com/cpro/ui/f.js"></script><script type="text/javascript">
/*pic*/
var cpro_id = "u1968034";
</script>
<script type="text/javascript" src="http://cpro.baidustatic.com/cpro/ui/i.js"></script>
</body>
</html>[root@yinzhengjie tmp]#
以上代码执行结果在这里
2.“net/url”模块用法展示;
“net/url”模块中我们会用到一个解析方法,它可以解析url使用的协议,主机名,当前路径,请求信息,用户信息,当前锚点等等。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"os"
"net/url"
"log"
"fmt"
) func main() {
s := os.Args[1]
u,err := url.Parse(s)
if err != nil {
log.Fatal(err)
}
fmt.Println("使用的协议是:",u.Scheme)
fmt.Println("主机名称是:",u.Host)
fmt.Println("当前路径是:",u.Path)
fmt.Println("请求信息是:",u.RawQuery)
fmt.Println("用户信息是:",u.User)
fmt.Println("当前的锚点是:",u.Fragment)
}
[root@yinzhengjie tmp]# more url.go
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"os"
"net/url"
"log"
"fmt"
) func main() {
s := os.Args[1]
u,err := url.Parse(s)
if err != nil {
log.Fatal(err)
}
fmt.Println("使用的协议是:",u.Scheme)
fmt.Println("主机名称是:",u.Host)
fmt.Println("当前路径是:",u.Path)
fmt.Println("请求信息是:",u.RawQuery)
fmt.Println("用户信息是:",u.User)
fmt.Println("当前的锚点是:",u.Fragment)
}
[root@yinzhengjie tmp]#
[root@yinzhengjie tmp]#
[root@yinzhengjie tmp]# go run url.go http://www.cnblogs.com/yinzhengjie/p/7202796.html
使用的协议是: http
主机名称是: www.cnblogs.com
当前路径是: /yinzhengjie/p/7202796.html
请求信息是:
用户信息是: <nil>
当前的锚点是:
[root@yinzhengjie tmp]#
以上代码用法展示戳这里
3.goquery模块用法展示;
做过 Web 开发的,应该都用过或听过 jQuery,它提供了方便的操作 DOM 的 API。使用 Go 语言做服务器端开发,有时候需要解析 HTML 文件,比如抓取网站内容、写一个爬虫等。这时候如果有一个类似 jQuery 的库可以使用,操作 DOM 会很方便,而且,上手也会很快。github.com/PuerkitoBio/goquery 这个库就实现了类似 jQuery 的功能,让你能方便的使用 Go 语言操作 HTML 文档。
首先,要强调一点,要执行以下代码需要安装一个goquery模块,该模块用来爬去网站的内容的简直就是一个神器啊。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"net/http"
"log"
"github.com/PuerkitoBio/goquery"
"fmt"
) /*
用goquery可以快速获取url的标签。
*/ func main() {
url := "http://www.xiaohuar.com" //定义一个URl,也就是我们要爬的网站。
resp,err := http.Get(url) ////获取url的内容,包括请求头信息都在里面放着的。 if err != nil {
log.Fatal(err)
}
defer resp.Body.Close() //千万要关闭链接,不然会造成资源泄露。
if resp.StatusCode != http.StatusOK{ //如果返回状态出现错误,就抛出错误并终止程序。
log.Fatal(resp.Status)
}
//io.Copy(os.Stdout,resp.Body) //将结果输出到屏幕上,并不会占用内存。
doc,err := goquery.NewDocumentFromResponse(resp) //对resp进行过滤,最终会拿到首页路径url
if err != nil {
log.Fatal(err)
}
doc.Find("img").Each(func(i int, s *goquery.Selection) { /*表示找到"img"的标签然后用Each方法遍历每一个图片路径。*/
link,ok := s.Attr("src") //找到“img”标签后会将其的“src”的路径找到,最终也就找到了图片的路径。
if ok {
fmt.Println(link) //如果存在“src”路径就打印
}else {
fmt.Println("抱歉,没有发现该路径。")
}
})
}
[root@yinzhengjie tmp]# more goquery.go
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"net/http"
"log"
"github.com/PuerkitoBio/goquery"
"fmt"
) /*
用goquery可以快速获取url的标签。
*/ func main() {
url := "http://www.xiaohuar.com" //定义一个URl,也就是我们要爬的网站。
resp,err := http.Get(url) ////获取url的内容,包括请求头信息都在里面放着的。 if err != nil {
log.Fatal(err)
}
defer resp.Body.Close() //千万要关闭链接,不然会造成资源泄露。
if resp.StatusCode != http.StatusOK{ //如果返回状态出现错误,就抛出错误并终止程序。
log.Fatal(resp.Status)
}
//io.Copy(os.Stdout,resp.Body) //将结果输出到屏幕上,并不会占用内存。
doc,err := goquery.NewDocumentFromResponse(resp) //对resp进行过滤,最终会拿到首页路径url
if err != nil {
log.Fatal(err)
}
doc.Find("img").Each(func(i int, s *goquery.Selection) { /*表示找到"img"的标签然后用Each方法遍历每一个图片路径。*/
link,ok := s.Attr("src") //找到“img”标签后会将其的“src”的路径找到,最终也就找到了图片的路径。
if ok {
fmt.Println(link) //如果存在“src”路径就打印
}else {
fmt.Println("抱歉,没有发现该路径。")
}
})
} [root@yinzhengjie tmp]#
[root@yinzhengjie tmp]#
[root@yinzhengjie tmp]# go run goquery.go
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
http://www.xiaohuar.com/skin/meizi/images/grey.gif
[root@yinzhengjie tmp]#
以上代码执行方式结果戳这里哟
如果你想要的代码扩展性更高,可以将main函数的代码定义成一个函数,这样在主函数调用该函数就好了,代码如下:
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"net/http"
"log"
"github.com/PuerkitoBio/goquery"
"fmt"
"errors"
) func fetch(url string) ([]string,error) {
var urls []string //定义一个空切片数组
resp,err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK{
return nil,errors.New(resp.Status) //表示当出现错误是,返回空列表,并将错误状态返回。
}
doc,err := goquery.NewDocumentFromResponse(resp)
if err != nil {
log.Fatal(err)
}
doc.Find("img").Each(func(i int, s *goquery.Selection) {
link,ok := s.Attr("src")
if ok {
urls = append(urls,link) //将过滤出来的图片路径都追加到urls的数组中去,最终返回给用户。
}else {
fmt.Println("抱歉,没有发现该路径。")
} })
return urls,nil
} func main() {
url := "http://m.xiaohuar.com" //定义一个URl,也就是我们要爬的网站。注意这里不要用www,而要用m,因为m是用手机端方式访问。而www是电脑方式访问。
urls,err := fetch(url)
if err != nil {
log.Fatal(err)
}
for _,u := range urls {
fmt.Println(u)
} }
[root@yinzhengjie tmp]# more goquery.go
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"net/http"
"log"
"github.com/PuerkitoBio/goquery"
"fmt"
"errors"
) func fetch(url string) ([]string,error) {
var urls []string //定义一个空切片数组
resp,err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK{
return nil,errors.New(resp.Status) //表示当出现错误是,返回空列表,并将错误状态返回。
}
doc,err := goquery.NewDocumentFromResponse(resp)
if err != nil {
log.Fatal(err)
}
doc.Find("img").Each(func(i int, s *goquery.Selection) {
link,ok := s.Attr("src")
if ok {
urls = append(urls,link) //将过滤出来的图片路径都追加到urls的数组中去,最终返回给用户。
}else {
fmt.Println("抱歉,没有发现该路径。")
} })
return urls,nil
} func main() {
url := "http://m.xiaohuar.com" //定义一个URl,也就是我们要爬的网站。注意这里不要用www,而要用m,因为m是用手机端方式访问。而w
ww是电脑方式访问。
urls,err := fetch(url)
if err != nil {
log.Fatal(err)
}
for _,u := range urls {
fmt.Println(u)
} } [root@yinzhengjie tmp]#
[root@yinzhengjie tmp]#
[root@yinzhengjie tmp]# go run goquery.go
http://www.xiaohuar.com/d/file/20170715/61110ba027f004fb503ff09cdee44d0c.jpg
http://www.xiaohuar.com/d/file/20170707/f7ca636f73937e33836e765b7261f036.jpg
http://www.xiaohuar.com/d/file/20170701/fb18711a6af87f30942d6a19f6da6b3e.jpg
http://www.xiaohuar.com/d/file/20170628/f3d06ef49965aedbe18286a2f221fd9f.jpg
http://www.xiaohuar.com/d/file/20170626/0ab1d89f54c90df477a90aa533ceea36.jpg
http://www.xiaohuar.com/d/file/20170619/e0456729d4dcbea569a1acbc6a47ab69.jpg
http://www.xiaohuar.com/d/file/20170612/1f6620771e2815d21f37d481fa8311e6.png
http://www.xiaohuar.com/d/file/20170604/ec3794d0d42b538bf4461a84dac32509.jpg
http://www.xiaohuar.com/d/file/20170603/e55f77fb3aa3c7f118a46eeef5c0fbbf.jpg
http://www.xiaohuar.com/d/file/20170603/c34b29f68e8f96d44c63fe29bf4a66b8.jpg
http://www.xiaohuar.com/d/file/20170529/e5902d4d3e40829f9a0d30f7488eab84.jpg
http://www.xiaohuar.com/d/file/20170529/8140c4ad797ca01f5e99d09c82dd8a42.jpg
http://www.xiaohuar.com/d/file/20170528/b352258c83776b9a2462277dec375d0c.jpg
http://www.xiaohuar.com/d/file/20170527/4a7a7f1e6b69f126292b981c90110d0a.jpg
http://www.xiaohuar.com/d/file/20170520/dd21a21751e24a8f161792b66011688c.jpg
http://www.xiaohuar.com/d/file/20170516/6e295fe48c33245be858c40d37fb5ee6.jpg
http://www.xiaohuar.com/d/file/20170513/6121e3e90ff3ba4c9398121bda1dd582.jpg
http://www.xiaohuar.com/d/file/20170512/45fc9ed5d92c66f369b66841c58bd183.jpg
[root@yinzhengjie tmp]#
以上代码执行结果戳这里哟
扩展:
我们在访问一个标签中的源数据,可能获取的路径不是完整路径,那么如何将获取的路径补充其应该对应的绝对路径呢?我们可以简单来实现一下,后期我们可以对其做一个优化。
/*
#!/usr/bin/env gorun
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/GO%E8%AF%AD%E8%A8%80%E7%9A%84%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/ package main import (
"errors"
"fmt"
"github.com/PuerkitoBio/goquery"
"log"
"net/http"
"net/url"
"os"
"strings"
) func fetch(url string) ([]string, error) { //改函数会拿到我们想要的图片的路径。
var urls []string //定义一个空切片数组
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New(resp.Status) //表示当出现错误是,返回空列表,并将错误状态返回。
}
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
log.Fatal(err)
}
doc.Find("img").Each(func(i int, s *goquery.Selection) {
link, ok := s.Attr("src")
if ok {
urls = append(urls, link) //将过滤出来的图片路径都追加到urls的数组中去,最终返回给用户。
} else {
fmt.Println("抱歉,没有发现该路径。")
} })
return urls, nil
} func Clean_urls(root_path string, picture_path []string) []string {
var Absolute_path []string //定义一个绝对路径数组。
url_info, err := url.Parse(root_path)
if err != nil {
log.Fatal(err)
}
Scheme := url_info.Scheme //获取到链接的协议
//fmt.Println("使用的协议是:",Scheme)
Host := url_info.Host //获取链接的主机名
//fmt.Println("主机名称是:",Host)
Path := url_info.Path //获取到链接的相对路径
Path_Directory := strings.Split(Path, "/")[1] //获取到存放文件的目录的路径“Path_Directory”。
//fmt.Println("存放文件的目录是:",Path_Directory)
for _, souce_path := range picture_path {
if strings.HasPrefix(souce_path, "https") { //如果当前当前路径是以“https”开头说明是绝对路径,因此我们给一行空代码,表示不执行任何操作,千万别写:“continue”,空着就好。 } else if strings.HasPrefix(souce_path, "//") { //判断当前路径是否以“//”开头(说明包含主机名)
souce_path = Scheme + ":" + souce_path //如果是就对其进行拼接操作。以下逻辑相同。
} else if strings.HasPrefix(souce_path, "/") { //说明不包含主机名和协议,我们进行拼接即可。
souce_path = Scheme + "://" + Host + souce_path
} else {
souce_path = Scheme + "://" + Host + "/" + Path_Directory + "/" + souce_path
}
Absolute_path = append(Absolute_path, souce_path) //不管是否满足上面的条件,最终都会被追加到该数组中来。
}
return Absolute_path //最终返回处理后的每个链接的绝对路基。
} func main() {
root_path := os.Args[1] //定义一个URl,也就是我们要爬的网站。
picture_path, err := fetch(root_path) //“fetch”函数会帮我们拿到picture_path的路径,但是路径可能是相对路径或是绝对路径。不同意。
if err != nil {
log.Fatal(err)
} Absolute_path := Clean_urls(root_path, picture_path) //“Clean_urls”函数会帮我们把picture_path的路径做一个统一,最终都拿到了绝对路径Absolute_path数组。 for _, Picture_absolute_path := range Absolute_path {
fmt.Println(Picture_absolute_path) //最终我们会得到一个图片的完整路径,我们可以对这个路径进行下载,压缩,加密等等操作。
}
}