如何解析具有压缩链接的站点地图索引
我编写了一个程序,该程序读取页面的 /robots.txt 和 /sitemap.xml 并减去可用的站点地图并将它们存储在 siteMapsUnsorted
列表中。
到达那里后,我使用crawler-commons库来分析链接是否是SiteMap或SiteMapIndexes(SiteMap集群)。
当我在正常的 siteMapIndex 上使用它时,它可以工作,在某些情况下,较大的网站具有压缩格式的 SiteMapIndexes 列表,例如:
- 压缩的 sitemapIndex: http://tripadvisor-sitemaps.s3-website-us-east-1.amazonaws.com/2/es/sitemap_es_index.xml
- 正常站点地图索引: https://www.infolibre.es/sitemap_index_382e2.xml
我正在使用的代码:
SiteMapParser sitemapParser = new SiteMapParser();
for (String sitemapURLStr : siteMapsUnsorted) {
AbstractSiteMap siteMapCandidate = sitemapParser.parseSiteMap(new URL(sitemapURLStr));
//AbstractSiteMap siteMapCandidate = sitemapParser.parseSiteMap("xml", content , new URL(sitemapURLStr));
// Check if the elements inside the list are SiteMapIndexes or SiteMaps, if they are SiteMapINDEXES, we need to break them down into SiteMaps
if (siteMapCandidate instanceof SiteMapIndex){
SiteMapIndex siteMapIndex = (SiteMapIndex) siteMapCandidate;
for (AbstractSiteMap aSiteMap : siteMapIndex.getSitemaps()){
if (aSiteMap instanceof SiteMap){
String siteMapString = aSiteMap.getUrl().toString();
System.out.println(siteMapString);
siteMaps.add(siteMapString);
} else{
LOG.warn("ignoring site map index inside site map index: " + aSiteMap.getUrl());
}
}
}
// If the elements inside the list are individual SiteMaps we add them to the SiteMaps list
else {
siteMaps.add(siteMapCandidate.getUrl().toString());
}
}
我注意到该方法 parseSitemap 根据您传递给它的参数而变化,但在尝试多次后我找不到处理压缩元素的方法。
我的最后一个选择是编写一个方法,下载每个 .tar.gz,解压缩它,读取解压缩的链接列表,存储它们,最后删除该目录;但这会极其缓慢且低效,所以首先我来这里看看是否有人有更好的想法/可以帮助我使用 parseSitemap()。
感谢任何人提前提供帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
失败的原因是 Tripadvisor 没有在其站点地图上设置正确的 mime 类型:
以及使用 仅在内容类型为以下之一时使用 gzip 进行解码
:可以通过更好地检测 gzip 和 xml(例如 URL 以
.xml.gz
结尾的 URL)来解决此问题,并在将站点地图下载到字节[]
。The reason this is failing is that Tripadvisor doesn't set the correct mime type on its sitemaps:
and the library that are using only decodes with gzip when the content type is one of:
You could probably work around this by implementing better detection of gzip and xml (like the URL ends in
.xml.gz
) and call theprocessGzippedXML
method directly after downloading the sitemap to abyte[]
.