我想从我的 C# 应用程序中读取包含站点地图 url 的 robots.txt 文件 (www.abcd.com/robots.txt) 的文本。如果 robots.txt 文件中存在站点地图 url,则我必须使用 if else 生成警报,然后它显示是,并且它不包含站点地图 url,然后它将显示否。
Robots.txt 文件文本如下所示:
# Crawlers Setup
User-agent: *
Disallow:
Crawl-delay: 10
# Website Sitemap
Sitemap: http://www.abcd.com/sitemap.xml
我如何从 robots.txt 文件中读取此站点地图文本,因为 robots.txt 也是一个链接,实际上不是文本文件。它是 www.abcd.com/robots.txt
请您参考如下方法:
您可以使用类似 RobotsTxt 的库(免责声明:此处为项目所有者)。示例:
string contentsOfRobotsTxtFile = new WebClient().DownloadString("uri");
Robots robots = Robots.Load(content);
var sitemaps = robots.Sitemaps;
它在 Nuget 上也可用; http://www.nuget.org/packages/RobotsTxt/
