使用Ruby Mechanize单击LinkedIn组页面上的“显示更多”链接

时间:2012-07-23 02:29:01

标签: ruby web-scraping mechanize

我已登录到Linkedin并使用Ruby Mechanize到达我的群组页面。我也能够在页面上检索问题列表。但是,我无法单击底部的“显示更多”链接,以便我可以查看整个页面,从而解决所有问题:

require 'rubygems'
require 'mechanize'
require 'open-uri'

a = Mechanize.new { |agent|
  # LinkedIn probably refreshes after login
  agent.follow_meta_refresh = true
}

a.get('http://linkedin.com/') do |home_page|
    my_page = home_page.form_with(:name => 'login') do |form|
    form.session_key  = '********'   #put you email ID
    form.session_password = '********'  #put your password here
  end.submit

mygroups_page = a.click(my_page.link_with(:text => /Groups/))

#puts mygroups_page.links

link_to_analyse = a.click(mygroups_page.link_with(:text => 'Semantic Web'))

link_to_test = link_to_analyse.link_with(:text => 'Show more...')

puts link_to_test.class

# link_to_analyse.search(".user-contributed .groups a").each do |item|

#   puts item['href']

#  end

end

虽然页面中存在文本“显示更多...”的链接,但我无法点击它.Link_to_test.class显示NilClass可能出现的问题是什么?

The part of the page I need to reach is:
<div id="inline-pagination">
        <span class="running-count">20</span>
        <span class="total-count">1134</span>
            <a href="groups?mostPopularList=&amp;gid=49970&amp;split_page=2&amp;ajax=ajax" class="btn-quaternary show-more-comments" title="Show more...">
              <span>Show more...</span>
              <img src="http://static01.linkedin.com/scds/common/u/img/anim/anim_loading_16x16.gif" width="16" height="16" alt="">
            </a>
      </div>

我需要点击显示更多...我可以使用links_with(:href =&gt; ..)但似乎无法正常工作。

2 个答案:

答案 0 :(得分:1)

新答案:

我刚检查了该组的页面源,似乎对于“显示更多”链接,它们实际上使用了三个句号,而不是省略号。

您是否尝试通过title属性定位链接?

link_to_analyse.link_with(:title => 'Show more...')

如果仍然无效,您是否尝试使用

转储页面上所有链接的文本
link_to_analyse.links.each do |link|
  puts link.text
end

----老答案不正确----

LinkedIn使用“Horizo​​ntal Ellipsis”Unicode字符(代码U + 2026)作为他们的链接“看起来”,就像他们最后有“......”一样。所以你的代码实际上并没有找到链接。

您需要的角色:http://www.fileformat.info/info/unicode/char/2026/index.htm

偷偷摸摸:)

编辑:要获得课程链接,您需要在链接文本中插入适当的Unicode字符,如下所示:

link_to_analyse.link_with(:text => 'Show more\u2026')

答案 1 :(得分:0)

锚内的标签会在锚文本周围创建一些空白区域。您可以通过以下方式解决这个问题:

link_to_analyse.link_with :text => /\A\s*Show more...\s*\Z/

但这样做可能还不错:

link_to_analyse.link_with :text => /Show more.../
相关问题