【问题标题】:Ruby code to check if website has a sitemap or not用于检查网站是否有站点地图的 Ruby 代码
【发布时间】:2012-07-02 18:58:30
【问题描述】:

我正在 Rails 中开发一个应用程序,它需要检查输入网站 URL 的站点地图是否存在?例如,如果用户输入http://google.com,那么它应该返回“Sitemap present”。我已经看到通常网站在其 URL 末尾有 /sitemap.xml 或 /sitemap 的解决方案。所以我试着检查一下使用 typhoeus gem,检查 URL(如 www.google.com/sitemap.xml 或 www.apple.com/sitemap)的 response.code,如果它返回 200 或 301,则站点地图存在,否则不存在。但我发现有些站点即使没有站点地图也会返回 301,它们会将其重定向到主页(例如 http://yournextleap.com/sitemap.xml),因此我没有得到确凿的结果。任何帮助都会非常棒。 这是我使用 typhoeus 检查站点地图的示例代码:

# the request object
request = Typhoeus::Request.new("http://apple.com/sitemap")

# Run the request via Hydra.
hydra = Typhoeus::Hydra.new

request.on_complete do |response|
  if response.code == 301
   p "success 301" # hell yeah
   elsif response.code == 200
    p  "Success 200"
   elsif response.code == 404
.   puts "Could not get a sitemap, something's wrong."  
    else
    p "check your input!!!!"
end 

【问题讨论】:

    标签: ruby-on-rails ruby sitemap typhoeus


    【解决方案1】:

    HTTP 响应状态码 301 Moved Permanently 用于 永久重定向。此状态代码应与 位置标头。 RFC 2616 指出:

    If a client has link-editing capabilities, it should update all references to the Request URI.
    The response is cachable.
    Unless the request method was HEAD, the entity should contain a small hypertext note with a hyperlink to the new URI(s).
    If the 301 status code is received in response to a request of any type other than GET or HEAD, the client must ask the user before redirecting.
    

    我认为您认为 301 响应表明曾经存在站点地图是不公平的。如果您正在检查是否存在 sitemap.xml 或站点地图目录,则预期的正确响应是 2XX。

    如果您坚持假设 3XX 请求指示重定向到站点地图,请按照重定向并添加逻辑来检查页面的 url(如果是主页)或页面的内容以查看是否它具有 XML 结构。

    【讨论】:

      【解决方案2】:

      站点地图也可能被压缩为sitemap.xml.gz——因此您可能还需要检查该文件名。此外,它可能具有指向许多其他子站点地图的索引文件,这些子站点地图的名称也可能不同。

      对于我的项目中的示例,我有:

      sitemap_index.xml.gz 
        -> sitemap_en1.xml.gz (english version of links)
        -> sitemap_pl1.xml.gz (polish version of links)
        -> images_sitemap1.xml.gz (only images sitemap)
      

      网站使用这些文件名 ping 搜索引擎,但有时它们也可能将它们包含在 /robots.txt 文件中,因此您可以尝试在那里寻找它们。例如http://google.com 在他们的文件末尾有这个:

      (看看站点地图的名称有多奇怪!)

      Sitemap: http://www.gstatic.com/s2/sitemaps/profiles-sitemap.xml
      Sitemap: http://www.google.com/hostednews/sitemap_index.xml
      Sitemap: http://www.google.com/ventures/sitemap_ventures.xml
      Sitemap: http://www.google.com/sitemaps_webmasters.xml
      Sitemap: http://www.gstatic.com/trends/websites/sitemaps/sitemapindex.xml
      Sitemap: http://www.gstatic.com/dictionary/static/sitemaps/sitemap_index.xml
      

      关于 301:您可以尝试伪装成 Google Bot 或其他爬虫。也许他们会重定向除机器人之外的所有人。但如果他们重定向每个人,你真的无能为力。

      【讨论】:

      • 我明白你的意思了,谢谢,但有没有其他方法可以为我的案子得出结论性的结果?
      • 如果你的意思是 301 响应代码,那么,不是真的。就像@sunny-j 解释的那样。你可能当然有他们不想与普通用户分享的东西,所以也许这是一个站点地图。
      • 谢谢。是的,我从@Sunny 的回复中了解了大约 301 响应代码。就我而言,我的意思是这个问题是否有任何替代解决方案?
      • 如果站点有一个类似a54gf343.xml 的站点地图并且没有将它包含在robots.txt 文件中,那么我想没有办法发现它,除非你是他们ping 的搜索引擎。您可以寻找普通名称,如 sitemap.xml、sitemap.xml.gz、sitemap_index... 等,或查看robots.txt 以发现常用站点地图 - 但您已经知道了。
      • 好的,知道了。感谢您的帮助
      猜你喜欢
      • 2012-07-03
      • 1970-01-01
      • 2015-01-09
      • 2021-07-30
      • 1970-01-01
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多