动态网站推广与搜索引擎优化初探
2008-09-07 19:14
399 查看
最近接触动态网站推广问题,查阅资料后发现:难!搜索引擎的蜘蛛爬虫(robot)MS主要通过目录浏览方式进行搜集,而动态网站页面很少,绝大部分都是动态生成的,要被搜索引擎收录比较困难。目前大致以下几种方式进行推广:一是在各大搜索引擎提交自己的网站;二是通过各种大大小小的分类目录;三是做链接交换和邮件推广;四是自己生成网站地图和robot文件(还有其它很多五花八门的手段),本文要描述的是第四种方式。
第一个是动态网站的各个链接生成网站地图(据报道google,microsoft和yahoo联合声明一个统一的标准sitemap 0.9,事实上目前只有google一家可以提交网站地图, 参见:http://www.google.com/support/webmasters/bin/answer.py?answer=40318&hl=zh_CN),站点地图范本如下:
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
< urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
< url>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
< loc>http://www.example.com/</loc>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
< lastmod>2005-01-01</lastmod>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
< changefreq>monthly</changefreq>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
< priority>0.8</priority>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
</url>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
</urlset>
我的做法是用一张表记录点击的链接,再写一个页面来生成网站地图(存储地图文件的目录需要目录写授权)文件Sitemap.xml,代码如下:
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
* 生成网站地图Sitemap.xml
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
* sid:网站代码
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
private void CreateXMLFile(string sid)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedBlock.gif)
...{
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
SqlParameter param1 = new SqlParameter("@SID", SqlDbType.VarChar, 20);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
param1.Value = sid;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedSubBlock.gif)
IDataParameter[] parameters = new IDataParameter[] ...{ param1 };
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
DbHelperSQL dbHelper = new DbHelperSQL(connStr);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
string outParams = "";
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
DataSet ds = dbHelper.RunProcedure("spGetSiteMap", parameters, "TmpSiteMapInfo", ref outParams);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
if (ds.Tables[0].Rows.Count > 0)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
string XMLSpace = "http://www.google.com/schemas/sitemap/0.9";
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
DateTime dt = System.DateTime.Now;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
XmlText xmltext;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
XmlElement xmlelem;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
// Create a new, empty document.
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
XmlDocument doc = new XmlDocument();
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
XmlDeclaration docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
doc.AppendChild(docNode);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
// Create and insert a new element.
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
XmlNode urlset = doc.CreateNode(XmlNodeType.Element, "urlset", XMLSpace);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
doc.AppendChild(urlset);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
foreach (DataRow dr in ds.Tables[0].Rows)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
// Create a nested element (with an attribute).
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
XmlElement url = doc.CreateElement("", "url", XMLSpace);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
urlset.AppendChild(url);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem = doc.CreateElement("", "loc", XMLSpace);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmltext = doc.CreateTextNode(dr["URL"].ToString());
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem.AppendChild(xmltext);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
url.AppendChild(xmlelem);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem = doc.CreateElement("", "lastmod", XMLSpace);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmltext = doc.CreateTextNode(string.Format("{0:u}", dt).Substring(0,10));
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem.AppendChild(xmltext);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
url.AppendChild(xmlelem);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem = doc.CreateElement("", "changefreq", XMLSpace);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
if (dr["Type"].ToString() == "1")
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmltext = doc.CreateTextNode("daily");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
else
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmltext = doc.CreateTextNode("monthly");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem.AppendChild(xmltext);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
url.AppendChild(xmlelem);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem = doc.CreateElement("", "priority", XMLSpace);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmltext = doc.CreateTextNode(dr["OrderNo"].ToString());
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem.AppendChild(xmltext);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
url.AppendChild(xmlelem);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
doc.Save(Server.MapPath("Sitemap.xml"));
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
return;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
上面的代码比较简单,一个要点是如果上面结点(比如:urlset)带有命名空间,则下层结点也一定要带,否则下层结点会自动带一个空的命名空间(好像与习惯思维相反,这点花了偶不少时间)。
第二个是robots(爬虫用的配置文件),也有相关标准,网上资料很多,下面是我写的生成robots文件的代码:
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
private void CreateRobotFile(string sid)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedBlock.gif)
...{
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
SqlParameter param1 = new SqlParameter("@SID", SqlDbType.VarChar, 20);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
param1.Value = sid;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedSubBlock.gif)
IDataParameter[] parameters = new IDataParameter[] ...{ param1 };
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
DbHelperSQL dbHelper = new DbHelperSQL(connStr);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
string outParams = "";
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
DataSet ds = dbHelper.RunProcedure("spGetSiteMap", parameters, "TmpSiteMapInfo", ref outParams);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
if (ds.Tables[0].Rows.Count > 0)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
FileStream fs = new FileStream(Server.MapPath("robots.txt"), FileMode.OpenOrCreate, FileAccess.Write);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
StreamWriter m_streamWriter = new StreamWriter(fs);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.Flush();
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
// 使用StreamWriter来往文件中写入内容
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
// 把richTextBox1中的内容写入文件
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("# Robots.txt file from http://www.hugesoft.net");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("# All robots will spider the domain");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("Sitemap: http://www.hugesoft.net/Sitemap.xml");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("User-agent: *");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("Disallow: ");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
foreach (DataRow dr in ds.Tables[0].Rows)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
string str = dr["URL"].ToString().ToLower();
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
int index = str.IndexOf("http://");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
if (index < 0)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
continue;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
index = str.IndexOf("/",index + 7);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
if (index < 0)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
continue;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
str = str.Substring(index);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("Allow: " + str);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
//关闭此文件
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.Flush();
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.Close();
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
由于我记录的是绝对URL,因此生成robots.txt时需要解析URL,去掉域名部分。别某些标记(比如:Sitemap,
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
Allow)不一定能被所有类型的robot支持。
robots.txt文件一定要放在网站根目录下,Sitemap.xml可以提交给google(目前只此一家)。
请关照偶的个人网站:http://www.hugesoft.net/,谢谢。
第一个是动态网站的各个链接生成网站地图(据报道google,microsoft和yahoo联合声明一个统一的标准sitemap 0.9,事实上目前只有google一家可以提交网站地图, 参见:http://www.google.com/support/webmasters/bin/answer.py?answer=40318&hl=zh_CN),站点地图范本如下:
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
< urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
< url>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
< loc>http://www.example.com/</loc>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
< lastmod>2005-01-01</lastmod>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
< changefreq>monthly</changefreq>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
< priority>0.8</priority>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
</url>
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
</urlset>
我的做法是用一张表记录点击的链接,再写一个页面来生成网站地图(存储地图文件的目录需要目录写授权)文件Sitemap.xml,代码如下:
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
* 生成网站地图Sitemap.xml
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
* sid:网站代码
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
private void CreateXMLFile(string sid)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedBlock.gif)
...{
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
SqlParameter param1 = new SqlParameter("@SID", SqlDbType.VarChar, 20);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
param1.Value = sid;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedSubBlock.gif)
IDataParameter[] parameters = new IDataParameter[] ...{ param1 };
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
DbHelperSQL dbHelper = new DbHelperSQL(connStr);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
string outParams = "";
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
DataSet ds = dbHelper.RunProcedure("spGetSiteMap", parameters, "TmpSiteMapInfo", ref outParams);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
if (ds.Tables[0].Rows.Count > 0)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
string XMLSpace = "http://www.google.com/schemas/sitemap/0.9";
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
DateTime dt = System.DateTime.Now;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
XmlText xmltext;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
XmlElement xmlelem;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
// Create a new, empty document.
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
XmlDocument doc = new XmlDocument();
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
XmlDeclaration docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
doc.AppendChild(docNode);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
// Create and insert a new element.
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
XmlNode urlset = doc.CreateNode(XmlNodeType.Element, "urlset", XMLSpace);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
doc.AppendChild(urlset);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
foreach (DataRow dr in ds.Tables[0].Rows)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
// Create a nested element (with an attribute).
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
XmlElement url = doc.CreateElement("", "url", XMLSpace);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
urlset.AppendChild(url);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem = doc.CreateElement("", "loc", XMLSpace);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmltext = doc.CreateTextNode(dr["URL"].ToString());
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem.AppendChild(xmltext);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
url.AppendChild(xmlelem);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem = doc.CreateElement("", "lastmod", XMLSpace);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmltext = doc.CreateTextNode(string.Format("{0:u}", dt).Substring(0,10));
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem.AppendChild(xmltext);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
url.AppendChild(xmlelem);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem = doc.CreateElement("", "changefreq", XMLSpace);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
if (dr["Type"].ToString() == "1")
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmltext = doc.CreateTextNode("daily");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
else
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmltext = doc.CreateTextNode("monthly");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem.AppendChild(xmltext);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
url.AppendChild(xmlelem);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem = doc.CreateElement("", "priority", XMLSpace);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmltext = doc.CreateTextNode(dr["OrderNo"].ToString());
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
xmlelem.AppendChild(xmltext);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
url.AppendChild(xmlelem);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
doc.Save(Server.MapPath("Sitemap.xml"));
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
return;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
上面的代码比较简单,一个要点是如果上面结点(比如:urlset)带有命名空间,则下层结点也一定要带,否则下层结点会自动带一个空的命名空间(好像与习惯思维相反,这点花了偶不少时间)。
第二个是robots(爬虫用的配置文件),也有相关标准,网上资料很多,下面是我写的生成robots文件的代码:
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
private void CreateRobotFile(string sid)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedBlock.gif)
...{
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
SqlParameter param1 = new SqlParameter("@SID", SqlDbType.VarChar, 20);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
param1.Value = sid;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedSubBlock.gif)
IDataParameter[] parameters = new IDataParameter[] ...{ param1 };
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
DbHelperSQL dbHelper = new DbHelperSQL(connStr);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
string outParams = "";
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
DataSet ds = dbHelper.RunProcedure("spGetSiteMap", parameters, "TmpSiteMapInfo", ref outParams);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
if (ds.Tables[0].Rows.Count > 0)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
FileStream fs = new FileStream(Server.MapPath("robots.txt"), FileMode.OpenOrCreate, FileAccess.Write);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
StreamWriter m_streamWriter = new StreamWriter(fs);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.Flush();
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
// 使用StreamWriter来往文件中写入内容
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
// 把richTextBox1中的内容写入文件
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("# Robots.txt file from http://www.hugesoft.net");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("# All robots will spider the domain");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("Sitemap: http://www.hugesoft.net/Sitemap.xml");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("User-agent: *");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("Disallow: ");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
foreach (DataRow dr in ds.Tables[0].Rows)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
string str = dr["URL"].ToString().ToLower();
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
int index = str.IndexOf("http://");
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
if (index < 0)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
continue;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
index = str.IndexOf("/",index + 7);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
if (index < 0)
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
continue;
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
str = str.Substring(index);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.WriteLine("Allow: " + str);
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
//关闭此文件
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.Flush();
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/InBlock.gif)
m_streamWriter.Close();
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
由于我记录的是绝对URL,因此生成robots.txt时需要解析URL,去掉域名部分。别某些标记(比如:Sitemap,
![](http://www.hugesoft.net/admin/Images/OutliningIndicators/None.gif)
Allow)不一定能被所有类型的robot支持。
robots.txt文件一定要放在网站根目录下,Sitemap.xml可以提交给google(目前只此一家)。
请关照偶的个人网站:http://www.hugesoft.net/,谢谢。
相关文章推荐
- 谈一谈动态网站的搜索引擎优化策略
- 网站推广方案之搜索引擎优化SEO全攻略
- 动态网站如何做搜索引擎优化
- 网站优化不等于搜索引擎优化SEO
- 单页面网站对搜索引擎优化有什么影响?
- 网站优化:从搜索引擎到社交网络的艰难转变
- 网站搜索引擎优化问题
- Mysql索引优化(动态网站优化)
- 网站优化:搜索引擎告诉你应该怎么做SEO
- SE0--搜索引擎网站收录大全--博客推广
- 网站seo搜索引擎优化被推捧的原因
- 网站SEO详解链接策略与搜索引擎优化
- 试验网站#1搜索引擎优化收录情况记录(断续运行)2007-11-7
- 试验网站#3搜索引擎优化收录情况记录2007-11-9
- 试验网站#1搜索引擎优化收录情况记录(断续运行)2007-11-21
- 试验网站#3搜索引擎优化收录情况记录2007-11-23
- 试验网站#1搜索引擎优化收录情况记录(断续运行)2007-11-25
- 试验网站#3搜索引擎优化收录情况记录2007-11-30
- 试验网站#3搜索引擎优化收录情况记录2007-12-2
- 试验网站#3搜索引擎优化收录情况记录2007-12-6