golang 爬虫(二)
2016-05-27 14:18
330 查看
通用的爬虫原理示意图:具体实现过程:1、开一个协程从待抓取url中获取url,进行网页获取,网页解析,然后入库;2、开另一个协程从已抓取url中获取url,进行获取其他url并且放入待抓取url队列中;网页解析在golang中,解析网页有几个比较好用的第三方库。比如:github.com/opesun/goquery,github.com/PuerkitoBio/goquery等等。github.com/PuerkitoBio/goquery功能强大,但在使用的时候,编译不过,当时比较懒就不用了。而使用github.com/opesun/goquery,该插件是在解析html节点时,使用jq类似的语法。下面介绍几个列子:
content,err:=goquery.ParseUrl(url) if err != nil{ panic(error) }1、打印整个html:fmt.Println(content.Html())2、获取class的内容:content.Find(".name").Text();3、获取id的内容:content.Find("#name").Text();4、当获取的内容中超过一条内容时:
sex:=content.Find("input[name='gender']:checked") for i:=0;i<sex.Length();i++{ if sex.Eq(i).Attr("checked") != "checked"{ continue; } if sex.Eq(i).Attr("value")=="0"{ info.sex = "女" } if sex.Eq(i).Attr("value")=="1"{ info.sex = "男" } }
获取其他待抓取网页
本次测试是爬知乎网的用户信息。
1、获取问题网址url;
2、获取对应问题网址的回答用户url;
需改进的地方
1、爬其他用户url的算法;
2、只有两个线程,分别为生产者和消费者。使用线程池。
3、控制生产者的速度与消费者的速度的算法。
相关文章推荐
- google Analytics API使用
- go文件操作
- golang中的面向对象的思想
- GoLang之协程
- Django基础(四)
- 【GDOI2016】SigmaGo 题解
- mongo文本搜索的一个例子
- golang select
- django中的模型为什么返回的是对象
- history.back()和history.go()的区别
- Django入门
- django-debug-tools 使用
- django-debug-tools 使用
- GoldenGate的mgr进程
- Google Code Jam 2016 Round 1C C
- Algorithm_DP
- Google Code Jam 2016 Round 1B B
- go开发的Web 压力测试工具:Boom
- Django的render_to_response返回的变量转义问题解决
- VigorRoads: Kickstarter模式开启