sitemap檔案產生的java實現思路(轉)

來源:互聯網
上載者:User
在網站運營尤其是SEO過程中,SiteMap的作用是不容忽視的。先來看一下SiteMap的協議及規範。 Google SiteMap Protocol是Google推出的一種網站地圖協議,此協議檔案在早期的Robots.txt檔案協議基礎上升級而成。在Google網站管理員工具中指出“加入了Google SiteMap檔案的網站將更有利於Google網頁爬行機器人的爬行索引,這樣將提高索引網站內容的效率和準確度”。SiteMap檔案協議應用了簡單的XML格式,一共用到6個標籤,其中關鍵標籤包括連結地址、更新時間、更新頻率和索引優先權。 Google SiteMap檔案產生後格式如下: <urlset xmlns=” http://www.google.com/schemas/sitemap/0.84″ ; > <url> <loc> http://ecvip.org<;/loc > <lastmod>2005-06-03</lastmod> <changefreq>always</changefreq> <priority>1.0</priority> </url> </urlset> 以上六個標籤的作用分別為: changefreq:頁面內容更新頻率:分always、hourly、daily、weekly、monthly、yearly幾種; lastmod:頁面最後修改時間:Googlebot會判斷下次是否會再次索引; loc:頁面永久連結地址:具體的連結地址; priority:相對於其他頁面的優先權:在0.0—1.0之間; url:相對於前4個標籤的父標籤:Webmaster希望展示在SiteMap檔案中的每一個連結都要用和包含; urlset:相對於前5個標籤的父標籤:定義此xml檔案的命名空間,相當於網頁檔案中的標籤一樣的作用。 由於本人近段時間帶領一批人在完成大型電子商務網站項目,而該項目是由J2EE架構完成,於是就考慮到是否可以讓Java代碼自助每天產生電子商務網站的Sitemap檔案。按照這種想法,於是就開始構思,擺在我面前一下出現三個問題:1,擷取網站所有連結。 2,產生XML檔案 3,定時調用 後兩種問題如果做過Java程式的人,應該比較好解決。而如何擷取網站所有連結,好像以前沒有做過這一塊,而這種思路應該跟現在的爬蟲技術有點想像,於是在網上找到nutch架構。這個架構其實以前就聽說過,還比較有名。於是發現網路上有朋友研究他其中原始碼,其中有一篇文章對第一個問題可以明顯解決。於是所有代碼架構選定,由httpclient+nekohtml+jdom+spring構成。在這裡截取部分擷取網站連結代碼 private static void getText(StringBuffer sb, Node node) { if (node.getNodeType() == Node.TEXT_NODE) { sb.append(node.getNodeValue());//取得結點值,即開始與結束標籤之間的資訊 } NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i <> getText(sb, children.item(0));//遞迴遍曆DOM樹 } } } private void getOutlinks(URL base, Map outlinks, Node node, int level) { if (node.getNodeType() == Node.ELEMENT_NODE) { if (“a”.equalsIgnoreCase(node.getNodeName())) { StringBuffer linkText = new StringBuffer(); getText(linkText, node); NamedNodeMap attrs = node.getAttributes(); String target = null; for (int i = 0; i <> if (“href”.equalsIgnoreCase(attrs.item(0).getNodeName())) { target = attrs.item(0).getNodeValue();//在DOM樹中,屬性是一個結點。 break; } } if (target != null) try { URL url = new URL(base, target); if (StringUtil.contains(url.toString(), siteUrl)) { String urlLink=urlParamDecoder(url.toString()); if (level == 1) { outlinks.put(url.toString(), new OutLink(urlLink, new Date(), “daily”, “0.9″, linkText.toString().trim(), level)); } else { outlinks.put(url.toString(), new OutLink(urlLink, new Date(), “daily”, “0.8″, linkText.toString().trim(), level)); } } } catch (MalformedURLException e) { logger.error(e); } } } NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i <> getOutlinks(base, outlinks, children.item(i), level);//遞迴遍曆DOM樹 } } } private Map getHttpNode(String url, int level) { //初始化 Map siteMap = new HashMap(); if (StringUtil.containsNone(url, siteUrl)) { return siteMap; } HttpClient client = new HttpClient(); //建立檔案片段對象 DocumentFragment node = new HTMLDocumentImpl().createDocumentFragment(); url = urlParamEncoder(url); if (proxyHost != null) { client.getHostConfiguration().setProxy(proxyHost, proxyPort); } client.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout); client.getHttpConnectionManager().getParams().setSoTimeout(soTimeout); GetMethod method = new GetMethod(url); method.addRequestHeader(“Content-Type”, “text/html; charset=utf-8″); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); try { int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new HttpException(“HttpStatusCode : ” + statusCode); } InputStream inputStream = method.getResponseBodyAsStream(); parser.setProperty(“http://cyberneko.org/html/properties/default-encoding”, “UTF-8″); parser.setFeature( “ http://cyberneko.org/html/features/scanner/script/strip-comment-delims“, true); parser.parse(new InputSource(inputStream), node); getOutlinks(new URL(url), siteMap, node, level); } catch (SAXException se) { logger.error(se); } catch (HttpException he) { logger.error(he); } catch (IOException ie) { logger.error(ie); } catch (Exception e) { logger.error(e); } finally { // 釋放串連 method.releaseConnection(); } //返回結果 return siteMap; } 其中在編寫代碼過程中,需要解決的問題有,網站如果有中文參數時,由於httpclient會報url參數錯誤,因此需要利用URLEncoder.encode加處理。同時利用jdom在產生xml檔案時,由於jdom產生帶有命名空間xml時,要把所有元素都帶上命名空間參數。 最後就是將產生 xml 檔案放在網站根目錄下建立一個目錄叫 sitemapxml 。然後向幾大知名的搜尋引擎提交 Sitemap 檔案啦。 Google

向Google提交網站地圖Sitemap: 通過下面的網址管理提交; http://www.google.com/webmasters/tools/ 使用Google帳號登陸後,驗證您的網站,然後就可以再“sitemap”頁面提交了 需要提交的您網站的sitemap地址為 http://$url/sitemapxml/sitemap.xml 雅虎中國

向雅虎中國提交網站地圖Sitemap,通過下面的網址管理提交 http://sitemap.cn.yahoo.com/mysites 向MSN提交網站地圖Sitemap,用URL直接提交:

http://api.moreover.com/ping?u=http%3A//$url/sitemapxml/sitemap.xml 向ASK提交網站地圖Sitemap,直接提交

http://submissions.ask.com/ping?sitemap=http%3A//$url/sitemapxml/sitemap.xml 向百度Baidu提交網站地圖Sitemap,沒辦法,現在百度不支援Sitemap。

但可通過 http://www.baidu.com/search/url_submit.html%E6%9D%A5%E6%8F%90%E4%BA%A4%E4%BD%A0%E7%9A%84%E7%BD%91%E5%9D%80。 百度會自行搜尋,通過robots.txt爬行你的sitemap,更新速度很快。 在你的網站robots.txt檔案中添加: sitemap:http:// $url /sitemapxml/ sitemap .xml http://$url/sitemap/index.html (如果沒有robots.txt檔案,請在網站根目錄下添加robots.txt檔案) 當然最後,如果你利用 XSLT 可以結合 xml 檔案,產生 html 形式的網站地圖。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.