[doc] Add allowed_domains to example spiders

This commit is contained in:
Cj Malone 2025-06-02 12:59:21 +01:00
parent 8f92a26636
commit defe7a4382
8 changed files with 18 additions and 0 deletions

View File

@ -29,6 +29,7 @@ https://quotes.toscrape.com, following the pagination:
class QuotesSpider(scrapy.Spider):
name = "quotes"
allowed_domains = ["quotes.toscrape.com"]
start_urls = [
"https://quotes.toscrape.com/tag/humor/",
]

View File

@ -16,6 +16,7 @@ Consider the following Scrapy spider below:
class MySpider(scrapy.Spider):
name = "myspider"
allowed_domains = ["example.com"]
start_urls = (
"http://example.com/page1",
"http://example.com/page2",

View File

@ -118,6 +118,7 @@ instance, which can be accessed and used like this:
class MySpider(scrapy.Spider):
name = "myspider"
allowed_domains = ["scrapy.org"]
start_urls = ["https://scrapy.org"]
def parse(self, response):
@ -136,6 +137,7 @@ Python logger you want. For example:
class MySpider(scrapy.Spider):
name = "myspider"
allowed_domains = ["scrapy.org"]
start_urls = ["https://scrapy.org"]
def parse(self, response):

View File

@ -344,6 +344,7 @@ errors if needed:
class ErrbackSpider(scrapy.Spider):
name = "errback_example"
allowed_domains = ["www.httpbin.org", "example.invalid"]
start_urls = [
"http://www.httpbin.org/", # HTTP 200 expected
"http://www.httpbin.org/status/404", # Not found error
@ -712,6 +713,7 @@ signals will stop the download of a given response. See the following example:
class StopSpider(scrapy.Spider):
name = "stop"
allowed_domains = ["docs.scrapy.org"]
start_urls = ["https://docs.scrapy.org/en/latest/"]
@classmethod
@ -889,6 +891,7 @@ method for this job. Here's an example spider which uses it:
class LoginSpider(scrapy.Spider):
name = "example.com"
allowed_domains = ["www.example.com"]
start_urls = ["http://www.example.com/users/login.php"]
def parse(self, response):

View File

@ -219,6 +219,7 @@ In a spider, settings are available through ``self.settings``:
class MySpider(scrapy.Spider):
name = "myspider"
allowed_domains = ["example.com"]
start_urls = ["http://example.com"]
def parse(self, response):
@ -2012,6 +2013,7 @@ In order to use the reactor installed by Scrapy:
class QuotesSpider(scrapy.Spider):
name = "quotes"
allowed_domains = ["quotes.toscrape.com"]
def __init__(self, *args, **kwargs):
self.timeout = int(kwargs.pop("timeout", "60"))
@ -2041,6 +2043,7 @@ which raises an exception, becomes:
class QuotesSpider(scrapy.Spider):
name = "quotes"
allowed_domains = ["quotes.toscrape.com"]
def __init__(self, *args, **kwargs):
self.timeout = int(kwargs.pop("timeout", "60"))

View File

@ -253,6 +253,7 @@ Here's an example of how you would call it from your spider:
class MySpider(scrapy.Spider):
name = "myspider"
allowed_domains = ["example.com", "example.org", "example.net"]
start_urls = [
"http://example.com",
"http://example.org",

View File

@ -66,6 +66,7 @@ Let's take an example using :ref:`coroutines <topics-coroutines>`:
class SignalSpider(scrapy.Spider):
name = "signals"
allowed_domains = ["quotes.toscrape.com"]
start_urls = ["https://quotes.toscrape.com/page/1/"]
@classmethod

View File

@ -312,6 +312,7 @@ Spiders can access arguments in their `__init__` methods:
class MySpider(scrapy.Spider):
name = "myspider"
allowed_domains = ["www.example.com"]
def __init__(self, category=None, *args, **kwargs):
super(MySpider, self).__init__(*args, **kwargs)
@ -329,6 +330,7 @@ The above example can also be written as follows:
class MySpider(scrapy.Spider):
name = "myspider"
allowed_domains = ["www.example.com"]
async def start(self):
yield scrapy.Request(f"http://www.example.com/categories/{self.category}")
@ -867,6 +869,7 @@ Simplest example: process all urls discovered through sitemaps using the
class MySpider(SitemapSpider):
allowed_domains = ["www.example.com"]
sitemap_urls = ["http://www.example.com/sitemap.xml"]
def parse(self, response):
@ -881,6 +884,7 @@ callback:
class MySpider(SitemapSpider):
allowed_domains = ["www.example.com"]
sitemap_urls = ["http://www.example.com/sitemap.xml"]
sitemap_rules = [
("/product/", "parse_product"),
@ -902,6 +906,7 @@ whose url contains ``/sitemap_shop``:
class MySpider(SitemapSpider):
allowed_domains = ["www.example.com"]
sitemap_urls = ["http://www.example.com/robots.txt"]
sitemap_rules = [
("/shop/", "parse_shop"),
@ -919,6 +924,7 @@ Combine SitemapSpider with other sources of urls:
class MySpider(SitemapSpider):
allowed_domains = ["www.example.com"]
sitemap_urls = ["http://www.example.com/robots.txt"]
sitemap_rules = [
("/shop/", "parse_shop"),