Scrapy, a fast high-level web crawling & scraping framework for Python.
Strip comments from robots.txt Sitemap lines (#8434)
Hygor luz0a0df7c3c64b
diff --git a/scrapy/utils/_sitemap.py b/scrapy/utils/_sitemap.pyindex 2a00ddf37..6e656f2fc 100644--- a/scrapy/utils/_sitemap.py+++ b/scrapy/utils/_sitemap.py@@ -1,13 +1,15 @@from __future__ import annotationsimport warnings-from io import BytesIO, StringIO+from io import BytesIOfrom typing import TYPE_CHECKING, Anyfrom urllib.parse import urljoinimport lxml.etree+from protego import Protegofrom scrapy.exceptions import ScrapyDeprecationWarning+from scrapy.robotstxt import decode_robotstxtif TYPE_CHECKING:from collections.abc import Iterable, Iterator@@ -102,28 +104,12 @@ def sitemap_urls_from_robots(base_url: str | None = None,) -> Iterable[str]:if isinstance(robots_text, bytes):- for line in BytesIO(robots_text):- if line.lstrip()[:8].lower() == b"sitemap:":- try:- url = line.partition(b":")[2].strip().decode()- except UnicodeDecodeError:- continue- yield urljoin(base_url or "", url)-+ robots_text = decode_robotstxt(robots_text)else:warnings.warn("Passing `str` type as `robots_text` is deprecated, use `bytes`",ScrapyDeprecationWarning,stacklevel=2,)- yield from _sitemap_urls_from_robots_str(robots_text, base_url)---def _sitemap_urls_from_robots_str(- robots_text: str,- base_url: str | None = None,-) -> Iterable[str]:- for line in StringIO(robots_text):- if line.lstrip()[:8].lower() == "sitemap:":- url = line.partition(":")[2].strip()- yield urljoin(base_url or "", url)+ for url in Protego.parse(robots_text).sitemaps:+ yield urljoin(base_url or "", url)diff --git a/tests/sample_data/test_site/sitemap-comments/robots.txt b/tests/sample_data/test_site/sitemap-comments/robots.txtnew file mode 100644index 000000000..13c096739--- /dev/null+++ b/tests/sample_data/test_site/sitemap-comments/robots.txt@@ -0,0 +1,3 @@+User-agent: *+Disallow:+Sitemap: sitemap.xml # generated sitemapdiff --git a/tests/sample_data/test_site/sitemap-comments/sitemap.xml b/tests/sample_data/test_site/sitemap-comments/sitemap.xmlnew file mode 100644index 000000000..73b9682d3--- /dev/null+++ b/tests/sample_data/test_site/sitemap-comments/sitemap.xml@@ -0,0 +1,4 @@+<?xml version="1.0" encoding="UTF-8"?>+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">+ <url><loc>/text</loc></url>+</urlset>diff --git a/tests/test_spider_sitemap.py b/tests/test_spider_sitemap.pyindex e8d840e21..0228a2638 100644--- a/tests/test_spider_sitemap.py+++ b/tests/test_spider_sitemap.py@@ -137,7 +137,7 @@ Sitemap: /sitemap-relative-url.xml"http://www.example.com/sitemap-relative-url.xml",]- def test_get_sitemap_urls_from_robotstxt_skips_invalid_utf8_urls(self):+ def test_get_sitemap_urls_from_robotstxt_invalid_utf8_urls(self):robots = (b"User-agent: *\n"b"Sitemap: http://example.com/\xff.xml\n"@@ -148,6 +148,7 @@ Sitemap: /sitemap-relative-url.xmlspider = self.spider_class("example.com")assert [req.url for req in spider._parse_sitemap(r)] == [+ "http://example.com/.xml","http://example.com/ok.xml",]@@ -462,6 +463,18 @@ Sitemap: /sitemap-relative-url.xmlitems, _ = await crawl_items(_Spider, mockserver)assert items == [{"url": mockserver.url("/text")}]+ @coroutine_test+ async def test_robots_txt_sitemap_comment(self, mockserver: MockServer):+ class _Spider(self.spider_class): # type: ignore[name-defined,misc]+ name = "robots_txt_sitemap_comment"+ sitemap_urls = [mockserver.url("/static/sitemap-comments/robots.txt")]++ def parse(self, response):+ yield {"url": response.url}++ items, _ = await crawl_items(_Spider, mockserver)+ assert items == [{"url": mockserver.url("/text")}]+@coroutine_testasync def test_robots_txt_requested_once(self, mockserver: MockServer):"""robots.txt should be downloaded only once, even though both