Class: Query::Result::BaiduMobile

Inherits:
Base
  • Object
show all
Defined in:
lib/query/result/baidu_mobile.rb

Instance Attribute Summary

Attributes inherited from Base

#baseuri, #pagenumber, #perpage

Instance Method Summary collapse

Methods inherited from Base

#initialize, #rank, #ranks_for, #whole

Constructor Details

This class inherits a constructor from Query::Result::Base

Instance Method Details

#ads_bottomObject



65
66
67
# File 'lib/query/result/baidu_mobile.rb', line 65

def ads_bottom
    []
end

#ads_rightObject



62
63
64
# File 'lib/query/result/baidu_mobile.rb', line 62

def ads_right
    []
end

#ads_topObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/query/result/baidu_mobile.rb', line 49

def ads_top
    id = 0
    result = []
    @page.search("div[@class='ec_wise_ad']/div").each do |div|
        id += 1
        href = div.search("span[@class='ec_site']").first.text
        href = "http://#{href}"
        title = div.search("a/text()").text.strip
        host = Addressable::URI.parse(URI.encode(href)).host
        result[id] = {'title'=>title,'href'=>href,'host'=>host}
    end
    result
end

#nextObject

下一页



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/query/result/baidu_mobile.rb', line 98

def next
    nextbutton = @page.xpath('//a[text()="下一页"]').first
    return nil if nextbutton.nil?
    url = URI.encode nextbutton['href']
    # puts url
    # p @baseuri
    # exit
    url = URI.join(@baseuri,url).to_s
    page = HTTParty.get(url)
    r = Query::Result::BaiduMobile.new(page)
    r.baseuri=url
    r.pagenumber=@pagenumber+1
    r
end

#ranksObject

返回当前页所有查询结果



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/query/result/baidu_mobile.rb', line 5

def ranks
    #如果已经赋值说明解析过,不需要重新解析,直接返回结果
    return @ranks unless @ranks.nil?
    @ranks = Hash.new
    @page.xpath('//div[@class="result"]').each do |result|
        href,text,host,is_mobile = '','','',false
        a = result.search("a").first
        is_mobile = true unless a.search("img").empty?
        host = result.search('[@class="site"]').first
        next if host.nil?
        host = host.text
        href = a['href']
        text = a.text
        id = href.scan(/&order=(\d+)&/)
        if id.empty?
            id = nil
        else
            id = id.first.first.to_i
            # id = (@pagenumber-1)*10+id
        end
=begin
        result.children.each do |elem|
            if elem.name == 'a'
                href = elem['href']
                id = elem.text.match(/^\d+/).to_s.to_i
                text = elem.text.sub(/^\d+/,'')
                text.sub!(/^\u00A0/,'')
            elsif elem['class'] == 'abs'
                elem.children.each do |elem2|
                    if elem2['class'] == 'site'
                        host = elem2.text
                        break
                    end
                end
            elsif elem['class'] == 'site'
                host == elem['href']
            end
        end
=end

        @ranks[id.to_s] = {'href'=>href,'text'=>text,'is_mobile'=>is_mobile,'host'=>host.sub(/\u00A0/,'')}
    end
    @ranks
end


68
69
70
# File 'lib/query/result/baidu_mobile.rb', line 68

def related_keywords
    @related_keywords ||= @page.search("div[@class='relativewords_info']/a").map{|a|a.text}
end