Baiduserp
此gem的目的是专门用来解析百度的搜索结果页.最主要的功能是能得到十个自然结果的排名和URL,以及左侧/右侧的广告的排名/URL.
目前这个只是一个基本能用的版本,可能会有各种各样的问题,欢迎提BUG.
Installation
1 系统要求
Linux或Mac. Linux最好使用新版本的Ubuntu或Fedora系列.
2 安装ruby环境
只支持ruby1.9及以上. 最好的安装ruby的方法是通过RVM,RVM的使用方法可以搜索一下,有很多教程.
在最新的Ubuntu或Fedora系列的Linux中,也可以通过apt-get或yum安装ruby1.9.
3 安装gem依赖
需要依赖nokogiri这个gem.而这个gem需要系统中的两个库. 所以在ubuntu或者fedora下需要
$ sudo apt-get install libxslt-dev libxslt libxml2-dev libxml2 # ubuntu
$ sudo yum install libxml2-devel libxml2 libxslt libxslt-devel # fedora
以上依赖安装完成后,
$ gem install nokogiri
4 最后我们安装 baiduserp gem
$ gem install baiduserp
Usage
ruby 代码示例
require 'baiduserp'
require 'open-uri'
require 'pp'
pp Baiduserp.search 'keyword'
pp Baiduserp.parse open(http://www.baidu.com/s?wd=keyword).read.encode('UTF-8')
另外为了方便非ruby程序使用以及一次性调试,也提供了命令行调用方法,可以通过JSON格式交换数据:
$ baiduserp -h
Usage:
1. baiduserp -s 'keyword' # search 'keyword' and print parse result
2. baiduserp -s 'keyword' -o output.json # -o means save result to a file
3. baiduserp -f 'file path' # parse html source code from file
4. baiduserp -s 'keyword' -j # search 'keyword' and print parse result in JSON format
-s, --search Keyword Search Keyword & Parse SERP
-j, --jsonprint Print result in JSON format
-o, --output Output Save Result to File in JSON format
-f, --file File Parse Local File
最终结果采用了哈希表和数组相互嵌套的数据结构,其中,每条结果的paid值中,0代表自然结果,1代表左侧广告,2代表右侧广告.结果示例如下:
$ baiduserp -s 香港
{:serp_results=>
[{:rank=>4001,
:url=>"Agoda.Com.Cn/Hong_Kong",
:title=>nil,
:content=>"Agoda.Com.Cn/Hong_Kong",
:paid=>1},
{:rank=>4002,
:url=>"WWW.XKCITS.COM///TEL0755-86178888",
:title=>nil,
:content=>"WWW.XKCITS.COM///TEL0755-86178888",
:paid=>1},
{:rank=>1,
:url=>"baike.baidu.com/view/2607.htm",
:title=>"香港_百度百科",
:content=>
"香港是一座繁华的国际化大都市。地处珠江以东,与广东省深圳市相接。1842年至1997年,香港曾经是英国的殖民地,1997年7月1日,依据中英政府共同签... 简介 - 历史概况 - 大事年表 - 地名来源 - 地理环境 - 更多>> baike.baidu.com/view/2607.htm 2013-2-5 ",
:paid=>0,
:mu=>"http://baike.baidu.com/view/2607.htm"},
{:rank=>2,
:url=>"www.gov.hk/",
:title=>"GovHK 香港政府一站通:本港居民",
:content=>
"香港政府为当地居民提供的资讯和服务,内容包括通讯及科技、文化、康乐及运动、教育及培训、就业、环境、政府、法律及治安、保健及医疗服务、房屋及社会服务、入境事务...",
:paid=>0},
{:rank=>3,
:url=>"www.mafengwo.cn/travel-scenic-spot/m...",
:title=>"2013香港旅游攻略,香港自助游攻略,蚂蜂窝香港出游攻略游记 - 蚂蜂窝",
:content=>
"2013香港旅游攻略,介绍了香港旅游景点、线路、美食、住宿、地图等香港旅游攻略信息,了解香港旅游如莎莎化妆品、迪士尼、购物天堂和美食小吃等自助游攻略信息来蚂蜂窝...",
:paid=>0},
{:rank=>4,
:url=>"image.baidu.com",
:title=>"香港_百度图片 - 举报图片",
:content=>"相关推荐:香港电影金像奖提名香港地图香港迪士尼乐园香港海洋公园",
:paid=>0,
:mu=>
"http://image.baidu.com/i?tn=baiduimage&ct=201326592&lm=-1&cl=2&fr=ala1&word=%CF%E3%B8%DB"},
{:rank=>5,
:url=>"lvyou.baidu.com/",
:title=>" 香港旅游攻略_百度旅游",
:content=>
" 香港是亚洲繁华的大都市,地区及国际金融中心之一,条件优越的天然深水港,1842年至1997年是英国的殖民地,1997年7月1日回归中国。\n作为“...,旅游旺季为秋冬季节最佳...",
:paid=>0,
:mu=>"http://lvyou.baidu.com/scene/view/79c0adc41efa15d8330ab4f5"},
{:rank=>6,
:url=>"map.baidu.com/",
:title=>"香港特别行政区地图",
:content=>nil,
:paid=>0,
:mu=>
"http://map.baidu.com/?newmap=1&s=s%26wd%3D%25E9%25A6%2599%25E6%25B8%25AF%25E7%2589%25B9%25E5%2588%25AB%25E8%25A1%258C%25E6%2594%25BF%25E5%258C%25BA%26c%3D2912&fr=alac0&from=alamap"},
{:rank=>7,
:url=>nil,
:title=>" 中国香港天气预报_春节期间未来一周天气_中国天气网 - 最近访问:",
:content=>nil,
:paid=>0,
:baiduopen=>1,
:mu=>"http://www.weather.com.cn/weather/101320101.shtml"},
{:rank=>8,
:url=>nil,
:title=>" 香港吧 百度贴吧 ",
:content=>nil,
:paid=>0,
:mu=>"http://tieba.baidu.com/f?kw=%CF%E3%B8%DB&fr=ala0"},
{:rank=>9,
:url=>nil,
:title=>"香港的最新相关信息",
:content=>
"联手香港知名餐企 陶然居赴港开店 新华网重庆频道 2小时前今后,在香港也能吃到正宗芋儿鸡和田螺了。严琦昨日告诉商报记者,陶然居已与香港金百加集团达成合作协议——今年,金百加到重庆发展,陶然居则在香港开...",
:paid=>0,
:mu=>"http://www.baidu.com/s?tn=baidurt&rtt=1&bsst=1&wd=%CF%E3%B8%DB"},
{:rank=>10,
:url=>"hongkong.cncn.com/",
:title=>"香港旅游攻略2011_香港旅游网",
:content=>
"香港欣欣旅游网,提供香港香港旅游景点推荐、2月香港旅游攻略、香港旅行社、香港旅游线路、香港酒店预订、香港旅游地图等出行指南及旅游服务●欣欣旅游网 CNCN.com 更新...",
:paid=>0},
{:paid=>2,
:rank=>1,
:url=>"www.gay688.com",
:title=>"香港旅游",
:content=>"在深圳找香港旅游,我亲身体验,觉着最好的还是国际香港旅游,"},
{:paid=>2,
:rank=>2,
:url=>"www.galy678.com",
:title=>"国旅香港 港澳游四天三晚贵..",
:content=>"香港深圳国旅,2013特别港澳游观光路线,港澳游天天出团.贴心的"},
{:paid=>2,
:rank=>3,
:url=>"Hotel.Qunar.Com",
:title=>"香港-香港订酒店,就上去哪儿..",
:content=>"订香港酒店?驴友真实点评,酒店高清图,实时优惠价格,出行乐无忧!"},
{:paid=>2,
:rank=>4,
:url=>"DaoDao.com",
:title=>"去香港,看真实用户点评,来Da..",
:content=>"来DaoDao.com看香港相关的380家酒店,更有120000条旅客真实点评."}],
:result_num=>100000000,
:baidubrand=>0,
:related_keywords=>
["香港天气",
"香港电影",
"香港大学",
"香港地图",
"苹果香港官网",
"香港地铁",
"香港中文大学",
"香港电视剧",
"香港旅游",
"苹果香港"]}
Contributing
欢迎大家帮忙协助继续完善这个gem:
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
或者可以到Issue页面提交问题,可以提BUG,新的需求,各种建议,等等.