我有一个包含 100 多个 html 文件的目录的网站。我希望爬虫爬取该目录的所有 html 文件。我已经在我的 robots.txt 中添加了以下句子:
Allow /DirName/*.html$
有什么方法可以将目录中的文件包含在 sitemap.xml 文件中,以便抓取目录中的所有 html 文件?像这样的东西:
<url>
<loc>MyWebsiteName/DirName/*.html</loc>
</url>
我有一个包含 100 多个 html 文件的目录的网站。我希望爬虫爬取该目录的所有 html 文件。我已经在我的 robots.txt 中添加了以下句子:
Allow /DirName/*.html$
有什么方法可以将目录中的文件包含在 sitemap.xml 文件中,以便抓取目录中的所有 html 文件?像这样的东西:
<url>
<loc>MyWebsiteName/DirName/*.html</loc>
</url>
它不允许使用通配符。如果您在服务器中运行 php,那么您可以列出目录中的所有文件并使用DirectoryIterator自动生成 sitemap.xml 。
// this is assume you have already a sitemap class.
$sitemap = new Sitemap;
// iterate the directory
foreach(new DirectoryIterator('/MyWebsiteName/DirName') as $directoryItem)
{
// Filter the item
if(!$directoryItem->isFile()) continue;
// New basic sitemap.
$url = new Sitemap_URL;
// Set arguments.
$url->set_loc(sprintf('/DirName/%1$s', $directoryItem->getBasename()))
->set_last_mod(1276800492)
->set_change_frequency('daily')
->set_priority(1);
// Add it to sitemap.
$sitemap->add($url);
}
// Render the output.
$response = $sitemap->render();
// Cache the output for 24 hours.
$cache->set('sitemap', $response, 86400);
// Output the sitemap.
echo $response;