Class: Query::Engine::BaiduMobile

Inherits:
Object
  • Object
show all
Includes:
Query::Engine
Defined in:
lib/query/engine/baidu_mobile.rb

Constant Summary collapse

Host =
'm.baidu.com'
Options =
{
  :headers => {"User-Agent" => 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5'}
}

Instance Attribute Summary

Attributes included from Query::Engine

#perpage

Class Method Summary collapse

Methods included from Query::Engine

indexed?

Class Method Details

.query(wd, params = {}) ⇒ Object

基本查询,相当于从搜索框直接输入关键词查询



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/query/engine/baidu_mobile.rb', line 11

def self.query(wd, params={})
  q = Array.new
  q << "word=#{URI.encode(wd)}"
  q << "rn=#{@perpage.to_i}" if @perpage
  # Join arguments
  params.each do |k, v|
    q << "#{k.to_s}=#{v.to_s}"
  end
  uri = URI::HTTP.build(:host=>Host,:path=>'/s',:query=>q.join('&'))
  # begin
    res = HTTParty.get(uri, Options)
    r = Query::Result::BaiduMobile.new(res)
    r.baseuri, r.options = uri, Options
    r
  # rescue Exception => e
      # warn "#{__FILE__} #{__LINE__} #{uri} fetch error: #{e.to_s}"
      # return false
  # end
end

.suggestions(query, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/query/engine/baidu_mobile.rb', line 31

def self.suggestions(query,options = {})
  require 'json'
  query = URI.encode(query)

  suggestions = HTTParty.get("https://m.baidu.com/su?callback=jsonp11&wd=#{query}",options).to_s
  suggestions = suggestions.force_encoding('GB18030').encode('UTF-8')
  suggestions = suggestions.split('s:[')[1].delete(']});').delete('"').split(',').uniq
end