baiduserp 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,29 +1,193 @@
1
1
  # Baiduserp
2
2
 
3
- TODO: Write a gem description
3
+ gem的目的是专门用来解析百度的搜索结果页.最主要的功能是能得到十个自然结果的排名和URL,以及左侧/右侧的广告的排名/URL.
4
+
5
+ 目前这个只是一个基本能用的版本,可能会有各种各样的问题,欢迎提BUG.
4
6
 
5
7
  ## Installation
6
8
 
7
- Add this line to your application's Gemfile:
9
+ 1. 系统要求
10
+
11
+ Linux或Mac. Linux最好使用新版本的Ubuntu或Fedora系列.
12
+
13
+ 2. 安装ruby环境
14
+
15
+ 只支持ruby1.9及以上. 最好的安装ruby的方法是通过[RVM](https://rvm.io/),RVM的使用方法可以搜索一下,有很多教程.
16
+
17
+ 在最新的Ubuntu或Fedora系列的Linux中,也可以通过apt-get或yum安装ruby1.9.
8
18
 
9
- gem 'baiduserp'
19
+ 3. 安装gem依赖
10
20
 
11
- And then execute:
21
+ 需要依赖nokogiri这个gem.而这个gem需要系统中的两个库.
22
+ 所以在ubuntu或者fedora下需要
12
23
 
13
- $ bundle
24
+ ```
25
+ $ sudo apt-get install libxslt-dev libxslt libxml2-dev libxml2 # ubuntu
26
+ $ sudo yum install libxml2-devel libxml2 libxslt libxslt-devel # fedora
27
+ ```
14
28
 
15
- Or install it yourself as:
29
+ 以上依赖安装完成后,
16
30
 
17
- $ gem install baiduserp
31
+ `$ gem install nokogiri`
32
+
33
+ 4. 最后我们安装 baiduserp gem
34
+
35
+ `$ gem install baiduserp`
18
36
 
19
37
  ## Usage
20
38
 
21
- TODO: Write usage instructions here
39
+ ruby 代码示例
40
+
41
+ ```
42
+ require 'baiduserp'
43
+ require 'open-uri'
44
+ require 'pp'
45
+
46
+ pp Baiduserp.search 'keyword'
47
+
48
+ pp Baiduserp.parse open(http://www.baidu.com/s?wd=keyword).read.encode('UTF-8')
49
+
50
+ ```
51
+
52
+ 另外为了方便非ruby程序使用以及一次性调试,也提供了命令行调用方法,可以通过JSON格式交换数据:
53
+
54
+ ```
55
+ $ baiduserp -h
56
+ Usage:
57
+ 1. baiduserp -s 'keyword' # search 'keyword' and print parse result
58
+ 2. baiduserp -s 'keyword' -o output.json # -o means save result to a file
59
+ 3. baiduserp -f 'file path' # parse html source code from file
60
+ 4. baiduserp -s 'keyword' -j # search 'keyword' and print parse result in JSON format
61
+ -s, --search Keyword Search Keyword & Parse SERP
62
+ -j, --jsonprint Print result in JSON format
63
+ -o, --output Output Save Result to File in JSON format
64
+ -f, --file File Parse Local File
65
+ ```
66
+
67
+ 最终结果采用了哈希表和数组相互嵌套的数据结构,其中,每条结果的paid值中,0代表自然结果,1代表左侧广告,2代表右侧广告.结果示例如下:
68
+
69
+ ```
70
+ $ baiduserp -s 香港
71
+ {:serp_results=>
72
+ [{:rank=>4001,
73
+ :url=>"Agoda.Com.Cn/Hong_Kong",
74
+ :title=>nil,
75
+ :content=>"Agoda.Com.Cn/Hong_Kong",
76
+ :paid=>1},
77
+ {:rank=>4002,
78
+ :url=>"WWW.XKCITS.COM///TEL0755-86178888",
79
+ :title=>nil,
80
+ :content=>"WWW.XKCITS.COM///TEL0755-86178888",
81
+ :paid=>1},
82
+ {:rank=>1,
83
+ :url=>"baike.baidu.com/view/2607.htm",
84
+ :title=>"香港_百度百科",
85
+ :content=>
86
+ "香港是一座繁华的国际化大都市。地处珠江以东,与广东省深圳市相接。1842年至1997年,香港曾经是英国的殖民地,1997年7月1日,依据中英政府共同签... 简介 - 历史概况 - 大事年表 - 地名来源 - 地理环境 - 更多>> baike.baidu.com/view/2607.htm 2013-2-5 ",
87
+ :paid=>0,
88
+ :mu=>"http://baike.baidu.com/view/2607.htm"},
89
+ {:rank=>2,
90
+ :url=>"www.gov.hk/",
91
+ :title=>"GovHK 香港政府一站通:本港居民",
92
+ :content=>
93
+ "香港政府为当地居民提供的资讯和服务,内容包括通讯及科技、文化、康乐及运动、教育及培训、就业、环境、政府、法律及治安、保健及医疗服务、房屋及社会服务、入境事务...",
94
+ :paid=>0},
95
+ {:rank=>3,
96
+ :url=>"www.mafengwo.cn/travel-scenic-spot/m...",
97
+ :title=>"2013香港旅游攻略,香港自助游攻略,蚂蜂窝香港出游攻略游记 - 蚂蜂窝",
98
+ :content=>
99
+ "2013香港旅游攻略,介绍了香港旅游景点、线路、美食、住宿、地图等香港旅游攻略信息,了解香港旅游如莎莎化妆品、迪士尼、购物天堂和美食小吃等自助游攻略信息来蚂蜂窝...",
100
+ :paid=>0},
101
+ {:rank=>4,
102
+ :url=>"image.baidu.com",
103
+ :title=>"香港_百度图片 - 举报图片",
104
+ :content=>"相关推荐:香港电影金像奖提名香港地图香港迪士尼乐园香港海洋公园",
105
+ :paid=>0,
106
+ :mu=>
107
+ "http://image.baidu.com/i?tn=baiduimage&ct=201326592&lm=-1&cl=2&fr=ala1&word=%CF%E3%B8%DB"},
108
+ {:rank=>5,
109
+ :url=>"lvyou.baidu.com/",
110
+ :title=>" 香港旅游攻略_百度旅游",
111
+ :content=>
112
+ " 香港是亚洲繁华的大都市,地区及国际金融中心之一,条件优越的天然深水港,1842年至1997年是英国的殖民地,1997年7月1日回归中国。\n作为“...,旅游旺季为秋冬季节最佳...",
113
+ :paid=>0,
114
+ :mu=>"http://lvyou.baidu.com/scene/view/79c0adc41efa15d8330ab4f5"},
115
+ {:rank=>6,
116
+ :url=>"map.baidu.com/",
117
+ :title=>"香港特别行政区地图",
118
+ :content=>nil,
119
+ :paid=>0,
120
+ :mu=>
121
+ "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"},
122
+ {:rank=>7,
123
+ :url=>nil,
124
+ :title=>" 中国香港天气预报_春节期间未来一周天气_中国天气网 - 最近访问:",
125
+ :content=>nil,
126
+ :paid=>0,
127
+ :baiduopen=>1,
128
+ :mu=>"http://www.weather.com.cn/weather/101320101.shtml"},
129
+ {:rank=>8,
130
+ :url=>nil,
131
+ :title=>" 香港吧 百度贴吧 ",
132
+ :content=>nil,
133
+ :paid=>0,
134
+ :mu=>"http://tieba.baidu.com/f?kw=%CF%E3%B8%DB&fr=ala0"},
135
+ {:rank=>9,
136
+ :url=>nil,
137
+ :title=>"香港的最新相关信息",
138
+ :content=>
139
+ "联手香港知名餐企 陶然居赴港开店 新华网重庆频道 2小时前今后,在香港也能吃到正宗芋儿鸡和田螺了。严琦昨日告诉商报记者,陶然居已与香港金百加集团达成合作协议——今年,金百加到重庆发展,陶然居则在香港开...",
140
+ :paid=>0,
141
+ :mu=>"http://www.baidu.com/s?tn=baidurt&rtt=1&bsst=1&wd=%CF%E3%B8%DB"},
142
+ {:rank=>10,
143
+ :url=>"hongkong.cncn.com/",
144
+ :title=>"香港旅游攻略2011_香港旅游网",
145
+ :content=>
146
+ "香港欣欣旅游网,提供香港香港旅游景点推荐、2月香港旅游攻略、香港旅行社、香港旅游线路、香港酒店预订、香港旅游地图等出行指南及旅游服务●欣欣旅游网 CNCN.com 更新...",
147
+ :paid=>0},
148
+ {:paid=>2,
149
+ :rank=>1,
150
+ :url=>"www.gay688.com",
151
+ :title=>"香港旅游",
152
+ :content=>"在深圳找香港旅游,我亲身体验,觉着最好的还是国际香港旅游,"},
153
+ {:paid=>2,
154
+ :rank=>2,
155
+ :url=>"www.galy678.com",
156
+ :title=>"国旅香港 港澳游四天三晚贵..",
157
+ :content=>"香港深圳国旅,2013特别港澳游观光路线,港澳游天天出团.贴心的"},
158
+ {:paid=>2,
159
+ :rank=>3,
160
+ :url=>"Hotel.Qunar.Com",
161
+ :title=>"香港-香港订酒店,就上去哪儿..",
162
+ :content=>"订香港酒店?驴友真实点评,酒店高清图,实时优惠价格,出行乐无忧!"},
163
+ {:paid=>2,
164
+ :rank=>4,
165
+ :url=>"DaoDao.com",
166
+ :title=>"去香港,看真实用户点评,来Da..",
167
+ :content=>"来DaoDao.com看香港相关的380家酒店,更有120000条旅客真实点评."}],
168
+ :result_num=>100000000,
169
+ :baidubrand=>0,
170
+ :related_keywords=>
171
+ ["香港天气",
172
+ "香港电影",
173
+ "香港大学",
174
+ "香港地图",
175
+ "苹果香港官网",
176
+ "香港地铁",
177
+ "香港中文大学",
178
+ "香港电视剧",
179
+ "香港旅游",
180
+ "苹果香港"]}
181
+ ```
22
182
 
23
183
  ## Contributing
24
184
 
185
+ 欢迎大家帮忙协助继续完善这个gem:
186
+
25
187
  1. Fork it
26
188
  2. Create your feature branch (`git checkout -b my-new-feature`)
27
189
  3. Commit your changes (`git commit -am 'Add some feature'`)
28
190
  4. Push to the branch (`git push origin my-new-feature`)
29
191
  5. Create new Pull Request
192
+
193
+ 或者可以到Issue页面提交问题,可以提BUG,新的需求,各种建议,等等.
@@ -95,10 +95,6 @@ module Baiduserp
95
95
  url
96
96
  end
97
97
 
98
- def parse_serp_title(id)
99
-
100
- end
101
-
102
98
  def parse_serp_content(id)
103
99
  get_content_safe(@doc.search("//table[@id='#{id}']//font[@size='-1']"))
104
100
  end
@@ -107,7 +103,7 @@ module Baiduserp
107
103
  result = Hash.new
108
104
  result[:rank] = id
109
105
  result[:url] = parse_serp_url(id)
110
- result[:title] = table.css('h3').first.content
106
+ result[:title] = get_content_safe(table.css('h3'))
111
107
  result[:content] = parse_serp_content(id)
112
108
 
113
109
  if id > 3000 # sem ads
@@ -1,3 +1,3 @@
1
1
  module Baiduserp
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baiduserp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-04 00:00:00.000000000 Z
12
+ date: 2013-02-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -40,7 +40,7 @@ files:
40
40
  - lib/baiduserp.rb
41
41
  - bin/baiduserp
42
42
  - README.md
43
- homepage: http://www.baidu.com/
43
+ homepage: https://github.com/mqzhang/baiduserp
44
44
  licenses: []
45
45
  post_install_message:
46
46
  rdoc_options: []