【问题标题】:Ruby+Anemone Web Crawler: regex to match URLs ending in a series of digitsRuby+Anemone Web Crawler:正则表达式匹配以一系列数字结尾的 URL
【发布时间】:2012-01-11 02:02:43
【问题描述】:

假设我正在尝试抓取一个网站并跳过一个这样结束的页面:

http://HIDDENWEBSITE.com/anonimize/index.php?page=press_and_news&subpage=20060117

我目前在 Ruby 中使用 Anemone gem 来构建爬虫。我正在使用 skip_links_like 方法,但我的模式似乎永远不会匹配。我试图使其尽可能通用,因此它不依赖于子页面,而仅依赖于 =2105925(数字)。

我尝试过/=\d+$//\?.*\d+$/,但它似乎不起作用。

这类似于Skipping web-pages with extension pdf, zip from crawling in Anemone,但我不能用数字代替扩展名。

此外,使用=\d+$ 模式对http://regexpal.com/ 进行测试将成功匹配http://misc.com/test/index.php?page=news&subpage=20060118

编辑:

这是我的全部代码。我想知道是否有人可以确切地看到问题所在。

require 'anemone'
...
Anemone.crawl(url, :depth_limit => 3, :obey_robots_txt => true) do |anemone|
  anemone.skip_links_like /\?.*\d+$/
  anemone.on_every_page do |page|
    pURL = page.url.to_s
    puts "Now checking: " + pURL
    bestGuess[pURL] = match_freq( manList, page.doc.inner_text )
    puts "Successfully checked"
  end
end

我的输出是这样的:

...
Now checking: http://MISC.com/about_us/index.php?page=press_and_news&subpage=20110711
Successfully checked
...

【问题讨论】:

    标签: ruby regex ruby-on-rails-3 web-crawler anemone


    【解决方案1】:
      Anemone.crawl(url, :depth_limit => 3, :obey_robots_txt => true, :skip_query_strings => true) do |anemone|
       anemone.on_every_page do |page|
         pURL = page.url.to_s
         puts "Now checking: " + pURL
          bestGuess[pURL] = match_freq( manList, page.doc.inner_text )
         puts "Successfully checked"
       end
     end
    

    【讨论】:

    • 效果很好,谢谢!虽然,它有点跳过重!一些有效页面作为查询字符串出现。我应该重写类中的代码吗?
    • 当我打开删除查询字符串时,它会删除 MISC.com/ANON/index.php?page=code_of_ethicsMISC.com/about/…。我希望它爬行前者而不是后者。我只希望它跳过最后有数字的页面。
    【解决方案2】:

    实际上/\?.*\d+$/ 有效:

    ~> irb
    > all systems are go wirble/hirb/ap/show <
    ruby-1.9.2-p180 :001 > "http://hiddenwebsite.com/anonimize/index.php?page=press_and_news&subpage=20060117".match /\?.*\d+$/
     => #<MatchData "?page=press_and_news&subpage=20060117"> 
    

    【讨论】:

    • 否则这一定是我的代码的问题。我似乎无法让它工作。
    猜你喜欢
    • 2015-08-01
    • 2015-10-10
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    相关资源
    最近更新 更多