3

私はいくつかのサイトを持っています。たとえばhttp://example.com
、次のようなURIのリストとしてサイトマップを生成したいと思います。

  • http://example.com/main
  • http://example.com/tags
  • http://example.com/tags/foo
  • http://example.com/tags/bar

私はそれのための良いアプリケーションを見つけました:iGooMap
iGooMapは必要なURIリストをテキストファイル(XMLファイルではなく)として生成することができます。
これが私が達成しようとしていることの視覚的表現です:

これが私が欲しいものです

このタイプのサイトマップをRuby( Railsではなく)で生成したいと思います。SiteMapGenerator
を見つけましたが、生成されるのは.XMLファイルのみですが、前述のように、テキストファイルが必要です。

特定のサイトのリンクのリストを作成するためのRubyのソリューションはありますか?

4

2 に答える 2

7

必要なのは、Rubyのサイトマップジェネレータではなく、RubyのWebスパイダーです。アネモネをお勧めします

require 'anemone'

links = []

Anemone.crawl("http://www.foo.com/") do |anemone|
  anemone.on_every_page do |page|
      links << page.url
  end
end

File.open('./link_list.txt', 'wb'){|f| f.write links.join("\n") }

link_list.txtこれにより、次の内容で呼び出されるファイルが生成されます。

http://www.foo.com/
http://www.foo.com/digimedia_privacy_policy.html

WombatSpidrPioneerなどもあります。


編集: @ChrisCummingsが提案しているように、重複を防ぐために、のSet代わりにを使用することをお勧めします。Arrayまた、リンクをアルファベット順に並べ替えることをお勧めします。これにより、出力ファイルが人間にとって読みやすくなります。

require 'anemone'
require 'set'

links = Set.new                                    # Set will prevent duplicates

Anemone.crawl("http://www.foo.com/") do |anemone|
  anemone.on_every_page do |page|
    links << page.url.to_s                         # to_s needed in order to sort
  end
end

File.open('./link_list.txt', 'wb') do |f|
  f.write links.sort.join("\n")                    # call to sort added
end
于 2012-11-07T12:11:19.967 に答える
4

sitemap_generatorたとえば、カスタムアダプタを使用して拡張できます。

require 'sitemap_generator'
require 'nokogiri'

module SitemapGenerator
  class TextFileAdapter
    def write(location, raw_data)
      # Ensure that the directory exists
      dir = location.directory
      if !File.exists?(dir)
        FileUtils.mkdir_p(dir)
      elsif !File.directory?(dir)
        raise SitemapError.new("#{dir} should be a directory!")
      end

      doc = Nokogiri::XML( raw_data )
      txt = doc.css('url loc').map(&:text).join("\n")

      open(location.path, 'wb') do |f|
        f.write(txt)
      end
    end
  end
end

SitemapGenerator::Sitemap.default_host = 'http://example.com'
SitemapGenerator::Sitemap.create(
  :adapter => SitemapGenerator::TextFileAdapter.new,
  :sitemaps_namer => SitemapGenerator::SitemapNamer.new(:sitemap, :extension => '.txt')
) do
  add '/home', :changefreq => 'daily', :priority => 0.9
  add '/contact_us', :changefreq => 'weekly'
end
SitemapGenerator::Sitemap.ping_search_engines

これにより、ファイルが作成されますpublic/sitemap1.txt

http://example.com
http://example.com/home
http://example.com/contact_us
于 2012-11-07T11:12:25.530 に答える