Fetch and Parse Process¶
When calling sitemap_tree_for_homepage(), USP will try several methods to find sitemaps and recurse through sub-sitemaps.
Broadly the process is as follows:
Attempt to fetch
https://example.org/robots.txtand parse forSitemap:statements. We considerrobots.txtto be an index-type sitemap (as it lists other sitemaps)Fetch and parse each discovered sitemap URL. If a sitemap is an index-type sitemap, recurse into it.
Try to fetch known sitemap locations like
/sitemap.xmland/sitemap_index.xml, excluding those already declared inrobots.txt.Create a top-level dummy sitemap to act as the parent of
robots.txtand discovered sitemaps.
All fetching is done through the SitemapFetcher class, which is responsible for fetching and choosing the appropriate parser for the content.
The fetcher then attempts to parse using the process shown in this flowchart:
Show Parse Flowchart
Non-XML documents are parsed directly with their respective parser. For XML documents, the XMLSitemapParser parses the document to determine the type of the XML document and select the appropriate parser (the concrete parser) to actually extract information.
XML documents are detected with a heuristic (the document, when leading whitespace is trimmed, starts with <) to avoid issues with incorrect content types.
Index-type parsers instantiate the appropriate class from usp.objects.sitemap and another SitemapFetcher to fetch each of their children. This allows a sitemap of one type (e.g. robots.txt) to contain sitemaps of another type (e.g. XML). Duplicate declarations of sub-sitemaps within the same index-type sitemap are ignored, but otherwise order is preserved.
Page-type parsers instantiate the appropriate class from usp.objects.sitemap and instantiate instances of their internal page class (e.g. PagesXMLSitemapParser.Page). These are not converted to the public class SitemapPage until the end of the fetch process. The order sub-sitemaps or pages are declare in is preserved.
Tree Construction¶
See also
Each parser instance returns an object inheriting from AbstractSitemap after the parse process (including any child fetch-and-parses), constructing the tree from the bottom up. The top IndexWebsiteSitemap is then created to act as the parent of robots.txt and all well-known-path discovered sitemaps.
Deduplication¶
During the parse process, some de-duplication is performed within each individual sitemap. In an index sitemap, only the first declaration of a sub-sitemap is fetched. In a page sitemap, only the first declaration of a page is included.
However, this means that if a sub-sitemap is declared in multiple index sitemaps, or a page is declared in multiple page sitemaps, it will be included multiple times.