Frequently Asked Questions
Scrapy provides a built-in mechanism for extracting data (called selectors) but you can easily use (or lxml) instead, if you feel more comfortable working with them. After all, they’re just parsing libraries which can be imported and used from any Python code.
In other words, comparing (or lxml) to Scrapy is like comparing to Django.
Can I use Scrapy with BeautifulSoup?
Yes, you can. As mentioned above, can be used for parsing HTML responses in Scrapy callbacks. You just have to feed the response’s body into a BeautifulSoup
object and extract whatever data you need from it.
Here’s an example spider using BeautifulSoup API, with lxml
as the HTML parser:
Note
BeautifulSoup
supports several HTML/XML parsers. See BeautifulSoup’s official documentation on which ones are available.
Did Scrapy “steal” X from Django?
Probably, but we don’t like that word. We think Django is a great open source project and an example to follow, so we’ve used it as an inspiration for Scrapy.
We believe that, if something is already done well, there’s no need to reinvent it. This concept, besides being one of the foundations for open source and free software, not only applies to software but also to documentation, procedures, policies, etc. So, instead of going through each problem ourselves, we choose to copy ideas from those projects that have already solved them properly, and focus on the real problems we need to solve.
We’d be proud if Scrapy serves as an inspiration for other projects. Feel free to steal from us!
Does Scrapy work with HTTP proxies?
Yes. Support for HTTP proxies is provided (since Scrapy 0.8) through the HTTP Proxy downloader middleware. See HttpProxyMiddleware.
How can I scrape an item with attributes in different pages?
See Passing additional data to callback functions.
Scrapy crashes with: ImportError: No module named win32api
You need to install pywin32 because of .
How can I simulate a user login in my spider?
See .
Does Scrapy crawl in breadth-first or depth-first order?
By default, Scrapy uses a ) queue for storing pending requests, which basically means that it crawls in DFO order. This order is more convenient in most cases.
If you do want to crawl in true , you can do it by setting the following settings:
DEPTH_PRIORITY = 1
SCHEDULER_DISK_QUEUE = 'scrapy.squeues.PickleFifoDiskQueue'
SCHEDULER_MEMORY_QUEUE = 'scrapy.squeues.FifoMemoryQueue'
While pending requests are below the configured values of CONCURRENT_REQUESTS, or CONCURRENT_REQUESTS_PER_IP, those requests are sent concurrently. As a result, the first few requests of a crawl rarely follow the desired order. Lowering those settings to 1
enforces the desired order, but it significantly slows down the crawl as a whole.
My Scrapy crawler has memory leaks. What can I do?
Also, Python has a builtin memory leak issue which is described in .
How can I make Scrapy consume less memory?
See previous question.
How can I prevent memory errors due to many allowed domains?
If your domain names are similar enough, use your own regular expression instead joining the strings in allowed_domains into a complex regular expression.
If you can , use pyre2 instead of Python’s to compile your URL-filtering regular expression. See issue 1908.
See also other suggestions at .
Note
Remember to disable scrapy.spidermiddlewares.offsite.OffsiteMiddleware when you enable your custom implementation:
SPIDER_MIDDLEWARES = {
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware': None,
'myproject.middlewares.CustomOffsiteMiddleware': 500,
Yes, see .
Why does Scrapy download pages in English instead of my native language?
Try changing the default request header by overriding the DEFAULT_REQUEST_HEADERS setting.
Where can I find some example Scrapy projects?
See Examples.
Can I run a spider without creating a project?
Yes. You can use the runspider command. For example, if you have a spider written in a my_spider.py
file you can run it with:
See command for more info.
I get “Filtered offsite request” messages. How can I fix them?
Those messages (logged with level) don’t necessarily mean there is a problem, so you may not need to fix them.
Those messages are thrown by the Offsite Spider Middleware, which is a spider middleware (enabled by default) whose purpose is to filter out requests to domains outside the ones covered by the spider.
For more info see: .
What is the recommended way to deploy a Scrapy crawler in production?
See .
Can I use JSON for large exports?
It’ll depend on how large your output is. See in JsonItemExporter documentation.
Can I return (Twisted) deferreds from signal handlers?
Some signals support returning deferreds from their handlers, others don’t. See the Built-in signals reference to know which ones.
What does the response status code 999 means?
999 is a custom response status code used by Yahoo sites to throttle requests. Try slowing down the crawling speed by using a download delay of 2
(or higher) in your spider:
class MySpider(CrawlSpider):
name = 'myspider'
download_delay = 2
# [ ... rest of the spider code ... ]
Or by setting a global download delay in your project with the DOWNLOAD_DELAY setting.
Can I call pdb.set_trace()
from my spiders to debug them?
Yes, but you can also use the Scrapy shell which allows you to quickly analyze (and even modify) the response being processed by your spider, which is, quite often, more useful than plain old pdb.set_trace()
.
Simplest way to dump all my scraped items into a JSON/CSV/XML file?
To dump into a JSON file:
scrapy crawl myspider -O items.json
To dump into a CSV file:
To dump into a XML file:
scrapy crawl myspider -O items.xml
For more information see
The __VIEWSTATE
parameter is used in sites built with ASP.NET/VB.NET. For more info on how it works see this page. Also, here’s an which scrapes one of these sites.
What’s the best way to parse big XML/CSV data feeds?
Parsing big feeds with XPath selectors can be problematic since they need to build the DOM of the entire feed in memory, and this can be quite slow and consume a lot of memory.
In order to avoid parsing all the entire feed at once in memory, you can use the functions xmliter
and csviter
from module. In fact, this is what the feed spiders (see ) use under the cover.
Does Scrapy manage cookies automatically?
Yes, Scrapy receives and keeps track of cookies sent by servers, and sends them back on subsequent requests, like any regular web browser does.
For more info see and CookiesMiddleware.
How can I see the cookies being sent and received from Scrapy?
Enable the COOKIES_DEBUG setting.
How can I instruct a spider to stop itself?
Raise the CloseSpider exception from a callback. For more info see: .
How can I prevent my Scrapy bot from getting banned?
See .
Should I use spider arguments or settings to configure my spider?
Both and settings can be used to configure your spider. There is no strict rule that mandates to use one or the other, but settings are more suited for parameters that, once set, don’t change much, while spider arguments are meant to change more often, even on each spider run and sometimes are required for the spider to run at all (for example, to set the start url of a spider).
To illustrate with an example, assuming you have a spider that needs to log into a site to scrape data, and you only want to scrape data from a certain section of the site (which varies each time). In that case, the credentials to log in would be settings, while the url of the section to scrape would be a spider argument.
I’m scraping a XML document and my XPath selector doesn’t return any items
You may need to remove namespaces. See Removing namespaces.
How to split an item into multiple items in an item pipeline?
Item pipelines cannot yield multiple items per input item. instead, and use its process_spider_output() method for this purpose. For example:
from copy import deepcopy
from itemadapter import is_item, ItemAdapter
class MultiplyItemsMiddleware:
def process_spider_output(self, response, result, spider):
for item in result:
if is_item(item):
adapter = ItemAdapter(item)
for _ in range(adapter['multiply_by']):
yield deepcopy(item)
Does Scrapy support IPv6 addresses?
Yes, by setting DNS_RESOLVER to scrapy.resolver.CachingHostnameResolver
. Note that by doing so, you lose the ability to set a specific timeout for DNS requests (the value of the setting is ignored).
How to deal with <class 'ValueError'>: filedescriptor out of range in select()
exceptions?
This issue to appear when running broad crawls in macOS, where the default Twisted reactor is twisted.internet.selectreactor.SelectReactor. Switching to a different reactor is possible by using the setting.
In some situations, it might be useful to stop the download of a certain response. For instance, sometimes you can determine whether or not you need the full contents of a response by inspecting its headers or the first bytes of its body. In that case, you could save resources by attaching a handler to the bytes_received or signals and raising a StopDownload exception. Please refer to the topic for additional information and examples.