Class: Query::Engine::Qihoo

Inherits:
Base
  • Object
show all
Defined in:
lib/query/engine/qihoo.rb

Constant Summary collapse

Host =
'www.so.com'

Instance Attribute Summary

Attributes inherited from Base

#perpage

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#indexed?

Class Method Details



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/query/engine/qihoo.rb', line 16

def self.related_keywords(wd)
    url = "http://rs.so.com/?callback=Search.relate.render&encodein=utf-8&encodeout=utf-8&q="+URI.encode(wd)
    # uri = URI.join("http://#{Host}/",URI.encode('s?q='+wd)).to_s
    page = HTTParty.get(url)
    json_str = page.body
    json_str = json_str.split("(")[1]
    return nil if json_str.nil?
    json_str = json_str.delete(');').strip
    parsed_json = JSON.parse(json_str)
    # each
    # parsed_json.map { |q| p q['q']}
    @related_keywords = parsed_json.map { |q| q['q'] }
    # @related_keywords ||= @page.search("//div[@id=\"rs\"]//tr//a").map{|keyword| keyword.text}
end

Instance Method Details

#query(wd) ⇒ Object

基本查询, 相当于在搜索框直接数据关键词查询



6
7
8
9
10
11
12
13
14
15
# File 'lib/query/engine/qihoo.rb', line 6

def query(wd)
    #用原始路径请求
    uri = URI.join("http://#{Host}/",URI.encode('s?q='+wd)).to_s
    page = HTTParty.get(uri)
    #如果请求地址被跳转,重新获取当前页的URI,可避免翻页错误
    uri = URI.join("http://#{Host}/",page.request.path).to_s
    r = Query::Result::Qihoo.new(page)
    r.baseuri = uri
    r
end