百度Sitemap生成器

 今天用了兩個小時, 爲無限影視https://www.88tv.org)開發了一個小工具, 用來生成baidu的sitemap。  方便用。 數據庫

由於該電影站的視頻內容詳情網頁的ID是自增加的,因此能夠按順序快速生成。 不用再寫爬蟲去一個一個連接爬了。 工具

1.  輸入URL模板, 注意{*}, 這個是用來放ID的。 網站

2.  ID區間,要生成多少到多少的頁面連接。ui

3.  排除ID: 排除這些ID。 url

4.  更新時間, 這是sitemap中的結構, 通常指該頁面的更新時間。  頻繁度=更新頻繁度, spa

5.  這工具加入了其它內容, 方便整理視頻欄目, 首頁及其它頁面的內容。 code

 

 

 

這個工具不用讀數據庫, 不用爬網站, 只要你的網站內容ID有規律就能用。 orm

示例附件下載在末尾。 視頻

 

預覽圖:xml

 

 

事件代碼:

            Common.PageInfo pg;
            if (cbChangefreq.Text == "")
            {
                MessageBox.Show("沒有選擇更新頻率.");
                return;
            }

            List<Common.PageInfo> list = new List<Common.PageInfo>();
            for (int i = (int)NuDMin.Value; i < NudMax.Value; i++)
            {
                if (((System.Collections.IList)rbtPC.Text.Split(',')).Contains(i.ToString()))
                {
                    continue;
                }
                pg = new Common.PageInfo();
                pg.loc = txtUrl.Text.Trim().Replace("{*}", i.ToString());
                pg.lastmod = txtUpdateDate.Text;
                pg.priority = nudPriority.Value.ToString();
                pg.changefreq = cbChangefreq.SelectedItem.ToString();
                list.Add(pg);
            }
            siteMap.url = list;
            string mySitemapStr = siteMap.GenerateSiteMapString(Application.StartupPath + "\\" + txtTemplate.Text.Trim());
            rtbTxt.Text = mySitemapStr;

 

 

輔助類:

using System;
using System.Collections.Generic;
using System.Text;

namespace Common
{

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// 生成站點地圖sitemap 
    /// </summary>
    public class SiteMap
    {
        public List<PageInfo> url
        {
            get;
            set;
        }

        /// <summary>
        /// 生成SiteMap字符串
        /// </summary>
        /// <returns></returns>
        public string GenerateSiteMapString(string file = "")
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.AppendLine("<urlset>");

            string text = "";
            #region MyRegion
            if (!string.IsNullOrWhiteSpace(file))
            {
                text = System.IO.File.ReadAllText(file);
            }
            sb.AppendLine(text);
            #endregion

            foreach (PageInfo pi in url)
            {
                sb.AppendLine("<url>");
                sb.AppendLine(string.Format("<loc>{0}</loc>", pi.loc));
                sb.AppendLine(string.Format("<lastmod>{0}</lastmod>", pi.lastmod));
                sb.AppendLine(string.Format("<changefreq>{0}</changefreq>", pi.changefreq));
                sb.AppendLine(string.Format("<priority>{0}</priority>", pi.priority));
                sb.AppendLine("</url>");
            }

            sb.AppendLine("</urlset>");
            return sb.ToString();
        }

        /// <summary>
        /// 保存Site文件
        /// </summary>
        /// <param name="FilePath">路徑</param>
        public void SaveSiteMap(string FilePath, string content)
        {
            using (StreamWriter m_streamWriter = new StreamWriter(FilePath, false, Encoding.UTF8))
            {
                m_streamWriter.Flush();
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
                m_streamWriter.Write(content);
            }

        }
    }

    public class PageInfo
    {
        /// <summary>
        /// 網址
        /// </summary>
        public string loc { get; set; }

        /// <summary>
        /// 最後更新時間
        /// </summary>
        public string lastmod { get; set; }

        /// <summary>
        /// 更新頻繁程度
        /// </summary>
        public string changefreq { get; set; }

        /// <summary>
        /// 優先級,0-1
        /// </summary>
        public string priority { get; set; }
    }
}

 

下載:https://files.cnblogs.com/files/jackrebel/BaiduSiteMap.zip

很是簡單的源代碼。

相關文章
相關標籤/搜索