Scrapy tutorial: your first spider, and how to add proxies
By the ProxyOnly team · 16 Nov 2026 · 5 min read
A tested Scrapy tutorial: a spider that follows pagination and exports JSON, the settings that keep a crawl polite, and two ways to send Scrapy's requests through a proxy.
Scrapy is a Python framework for crawling websites and extracting data. Where Requests and BeautifulSoup leave the loop, the waiting and the retries to you, Scrapy runs them for you: it schedules requests, downloads several at once, retries failures, obeys robots.txt if you ask, and writes the results to a file. This tutorial builds a working spider, makes it polite, and adds a proxy.
We ran everything on 16 November 2026 with Scrapy 2.19.0 on Python 3.10 against quotes.toscrape.com, a site made for scraping practice. The output shown is what Scrapy printed.
Install
pip install scrapyYour first spider
A spider is a class with a name, the addresses to start from, and a parse method that receives each downloaded page. This one reads every quote on a page, yields it as an item, and follows the "Next" link until there is none.
import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = ["https://quotes.toscrape.com/"]
custom_settings = {
"ROBOTSTXT_OBEY": True,
"DOWNLOAD_DELAY": 1,
"AUTOTHROTTLE_ENABLED": True,
"USER_AGENT": "quotes-tutorial/1.0 (+https://proxyonly.com/blog)",
}
def parse(self, response):
for quote in response.css("div.quote"):
yield {
"text": quote.css("span.text::text").get(),
"author": quote.css("small.author::text").get(),
"tags": quote.css("a.tag::text").getall(),
}
next_page = response.css("li.next a::attr(href)").get()
if next_page:
yield response.follow(next_page, callback=self.parse)Save it as quotes_spider.py and run it without creating a project:
scrapy runspider quotes_spider.py -O quotes.json-O writes the items to a file, replacing it if it exists, and the format follows the extension. These lines from the log and the closing statistics tell the story of the run:
[scrapy.core.engine] DEBUG: Crawled (404) <GET https://quotes.toscrape.com/robots.txt> (referer: None)
[scrapy.extensions.feedexport] INFO: Stored json feed (100 items) in: quotes.json
'downloader/request_count': 11,
'downloader/response_status_count/200': 10,
'elapsed_time_seconds': 20.67855262500234,
'finish_reason': 'finished',
'item_scraped_count': 100,Scrapy fetched robots.txt first, which this site does not have, then ten pages, and wrote 100 items in about 21 seconds. The first item:
{"text": "“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”", "author": "Albert Einstein", "tags": ["change", "deep-thoughts", "thinking", "world"]}response.follow accepts the relative link from the page and resolves it for you. The selectors are CSS with Scrapy's ::text and ::attr() extensions, and XPath works too, as our XPath and CSS selector cheat sheet shows.
The settings that make a crawl polite
ROBOTSTXT_OBEYmakes Scrapy read and follow the site'srobots.txt. Set it explicitly. Scrapy's settings documentation notes that the built-in fallback isFalsefor historical reasons, while projects created withscrapy startprojectturn it on.DOWNLOAD_DELAYwaits between requests to the same site. Our crawl took about 21 seconds for 11 requests because of it, which is the point.AUTOTHROTTLE_ENABLEDadjusts the delay to how quickly the site responds, backing off when it slows down.USER_AGENTnames your bot and gives a URL where a site owner can read about it.
These matter more than any proxy. A crawler that hits a site hard gets blocked, and deserves to. Rate limiting explained covers what sites watch for.
For a bigger job, scrapy startproject creates a project with a settings file, item pipelines for cleaning and saving data, and room for several spiders. The single file above is enough to learn the moving parts.
Add a proxy
Scrapy's built-in HttpProxyMiddleware handles proxies, and its documentation describes two ways to use it.
The first is environment variables. It reads http_proxy, https_proxy and no_proxy, so this sends the whole crawl through one proxy without touching the code. We ran it with our test proxy in place of the placeholders, and the proxy logged the tunnel to quotes.toscrape.com.
https_proxy="http://USERNAME:PASSWORD@HOST:PORT" scrapy runspider quotes_spider.py -O quotes.jsonThe second is the proxy key in a request's meta, which lets you choose a proxy per request and takes precedence over the environment variables. This spider sets it on the first request and on every followed page. In current versions of Scrapy, the first requests of a crawl come from an async def start() method, which is what the spider documentation describes.
import scrapy
PROXY = "http://demo:s3cret@127.0.0.1:8101"
class QuotesProxySpider(scrapy.Spider):
name = "quotes_proxy"
custom_settings = {"DOWNLOAD_DELAY": 1, "CLOSESPIDER_PAGECOUNT": 2}
async def start(self):
yield scrapy.Request("https://quotes.toscrape.com/", meta={"proxy": PROXY})
def parse(self, response):
self.logger.info("page %s gave %d quotes", response.url, len(response.css("div.quote")))
next_page = response.css("li.next a::attr(href)").get()
if next_page:
yield response.follow(next_page, callback=self.parse, meta={"proxy": PROXY})We ran it against a test proxy on our own machine that requires a login.
[quotes_proxy] INFO: page https://quotes.toscrape.com/ gave 10 quotes
[quotes_proxy] INFO: page https://quotes.toscrape.com/page/2/ gave 10 quotesThe proxy logged a single CONNECT quotes.toscrape.com:443 for both pages, because Scrapy reused the tunnel it had already opened. That is efficient, and it also means that requests to the same site share one exit until the connection closes. With your own proxies, put the URL from your order page in place of PROXY. To spread a crawl across several static proxies, choose one per request, for example with itertools.cycle as in our Python Requests guide, and set it in each request's meta.
If the password contains characters such as @, : or /, percent-encode it in the URL. A 407 in the log means the proxy rejected the login, and every common proxy error covers the rest.
Questions people ask
Does Scrapy run JavaScript?
No. Scrapy downloads the HTML that the server sends and does not run the page's scripts. If the data appears only after JavaScript runs, look for the JSON request the page makes and call it with Scrapy, or use a browser tool for those pages.
How do I use several proxies in Scrapy?
Set the proxy key in each request's meta, choosing from your list as you create the request. Requests to the same site can share a connection, as the test showed, so spreading load is more reliable across sites or over time than request by request.
What is the difference between scrapy runspider and scrapy crawl?
scrapy runspider runs a spider from a single file, as in this tutorial. scrapy crawl runs a spider by name inside a project created with scrapy startproject, which also gives you a settings file and item pipelines.
Where Scrapy fits
Scrapy suits crawls of many pages: whole catalogues, archives, or many sites on a schedule. For a handful of pages, Requests and BeautifulSoup are simpler, as our BeautifulSoup tutorial shows. For pages that build their content with JavaScript, Scrapy needs help from a browser. Web scraping tools compared lays out the options, and the Scrapy setup guide has the proxy settings in short form.
Try it yourself
Create an account, add funds from US$1 and order a proxy in the country you need.
Keep reading
25 Nov 2026 · Tutorials
Python HTTP clients compared: requests vs httpx vs aiohttp
A tested comparison of requests, httpx and aiohttp, including a benchmark where the run-to-run noise turned out to be larger than the gap people argue about.
25 Nov 2026 · Tutorials
How to find your proxy server address and port
Find the proxy your machine is already configured to use on Windows, macOS, Linux and in browsers, or find the host and port of a proxy you bought.