diff --git a/scrapy/trunk/docs/faq.rst b/scrapy/trunk/docs/faq.rst index 37690590b..2661f4ff1 100644 --- a/scrapy/trunk/docs/faq.rst +++ b/scrapy/trunk/docs/faq.rst @@ -66,4 +66,7 @@ You need to install `pywin32`_ because of `this Twisted bug`_. .. _pywin32: http://sourceforge.net/projects/pywin32/ .. _this Twisted bug: http://twistedmatrix.com/trac/ticket/3707 +How can I simulate a user login in my spider? +--------------------------------------------- +See :ref:`ref-request-userlogin`. diff --git a/scrapy/trunk/docs/ref/index.rst b/scrapy/trunk/docs/ref/index.rst index c8516067c..62b7ac591 100644 --- a/scrapy/trunk/docs/ref/index.rst +++ b/scrapy/trunk/docs/ref/index.rst @@ -6,7 +6,6 @@ API Reference This section documents the Scrapy |version| API. For more information see :ref:`misc-api-stability`. .. toctree:: - :maxdepth: 2 spiders selectors diff --git a/scrapy/trunk/docs/ref/request-response.rst b/scrapy/trunk/docs/ref/request-response.rst index 5f59a3fba..b514f1d22 100644 --- a/scrapy/trunk/docs/ref/request-response.rst +++ b/scrapy/trunk/docs/ref/request-response.rst @@ -15,17 +15,17 @@ sites. Typically, :class:`Request` objects are generated in the spiders and pass across the system until they reach the Downloader, which executes the request -and returns a :class:`Response` object which goes back to the spider that -generated the request. +and returns a :class:`Response` object which travels back to the spider that +issued the request. -Both Request and Response classes contains subclasses which adds additional -functionality not required in the base classes. See -:ref:`ref-request-subclasses` and :ref:`ref-response-subclasses` below. +Both :class:`Request` and :class:`Response` classes have subclasses which adds +additional functionality not required in the base classes. These are described +below in :ref:`ref-request-subclasses` and :ref:`ref-response-subclasses`. Request objects =============== -.. class:: Request(url, callback=None, method='GET', body=None, headers=None, cookies=None, meta=None, encoding='utf-8', dont_filter=False, errback=None) +.. class:: Request(url[, callback, method, body, headers, cookies, meta, encoding, dont_filter, errback]) A :class:`Request` object represents an HTTP request, which is usually generated in the Spider and executed by the Downloader, and thus generating @@ -37,7 +37,8 @@ Request objects request (once its downloaded) as its first parameter. For more information see :ref:`ref-request-callback-arguments` below. - ``method`` is a string with the HTTP method of this request + ``method`` is a string with the HTTP method of this request, and defaults + to ``'GET'``. ``meta`` is a dict containing the initial values for the :attr:`Request.meta` attribute. If passed, the dict will be shallow copied. @@ -67,9 +68,9 @@ Request objects cookies={currency: 'USD', country: 'UY'}, meta={'dont_merge_cookies': True}) - ``encoding`` is a string with the encoding of this request. This encoding - will be used to percent-encode the URL and to convert the body to str (when - given as unicode). + ``encoding`` is a string with the encoding of this request (defaults to + ``'utf-8'``). This encoding will be used to percent-encode the URL and to + convert the body to str (when given as unicode). ``dont_filter`` is a boolean which indicates that this request should not be filtered by the scheduler. This is used when you want to perform an @@ -80,76 +81,71 @@ Request objects with 404 HTTP errors and such. , it receives a `Twisted Failure`_ instance as first parameter. -.. _Twisted Failure: http://twistedmatrix.com/documents/8.2.0/api/twisted.python.failure.Failure.html + .. _Twisted Failure: http://twistedmatrix.com/documents/8.2.0/api/twisted.python.failure.Failure.html -Request Attributes ------------------- + .. attribute:: Request.url -.. attribute:: Request.url + A string containing the URL of this request. Keep in mind that this + attribute contains the escaped URL, so it can differ from the URL passed in + the constructor. - A string containing the URL of this request. Keep in mind that this - attribute contains the escaped URL, so it can differ from the URL passed in - the constructor. + .. attribute:: Request.method -.. attribute:: Request.method + A string representing the HTTP method in the request. This is guaranteed to + be uppercase. Example: ``"GET"``, ``"POST"``, ``"PUT"``, etc - A string representing the HTTP method in the request. This is guaranteed to - be uppercase. Example: ``"GET"``, ``"POST"``, ``"PUT"``, etc + .. attribute:: Request.headers -.. attribute:: Request.headers + A dictionary-like object which contains the request headers. - A dictionary-like object which contains the request headers. + .. attribute:: Request.body -.. attribute:: Request.body + A str that contains the request body - A str that contains the request body + .. attribute:: Request.meta -.. attribute:: Request.meta + A dict that contains arbitrary metadata for this request. This dict is + empty for new Requests, and is usually populated by different Scrapy + components (extensions, middlewares, etc). So the data contained in this + dict depends on the extensions you have enabled. - A dict that contains arbitrary metadata for this request. This dict is - empty for new Requests, and is usually populated by different Scrapy - components (extensions, middlewares, etc). So the data contained in this - dict depends on the extensions you have enabled. + This dict is `shallow copied`_ when the request is cloned using the + ``copy()`` or ``replace()`` methods. - This dict is `shallow copied`_ when the request is cloned using the - ``copy()`` or ``replace()`` methods. + .. _shallow copied: http://docs.python.org/library/copy.html -.. _shallow copied: http://docs.python.org/library/copy.html + .. attribute:: Request.cache -.. attribute:: Request.cache + A dict that contains arbitrary cached data for this request. This dict is + empty for new Requests, and is usually populated by different Scrapy + components (extensions, middlewares, etc) to avoid duplicate processing. So + the data contained in this dict depends on the extensions you have enabled. - A dict that contains arbitrary cached data for this request. This dict is - empty for new Requests, and is usually populated by different Scrapy - components (extensions, middlewares, etc) to avoid duplicate processing. So - the data contained in this dict depends on the extensions you have enabled. + Unlike the ``meta`` attribute, this dict is not copied at all when the + request is cloned using the ``copy()`` or ``replace()`` methods. - Unlike the ``meta`` attribute, this dict is not copied at all when the - request is cloned using the ``copy()`` or ``replace()`` methods. + .. method:: Request.copy() -Request Methods ---------------- + Return a new Request which is a copy of this Request. The attribute + :attr:`Request.meta` is copied, while :attr:`Request.cache` is not. See also + :ref:`ref-request-callback-arguments`. -.. method:: Request.copy() + .. method:: Request.replace([url, callback, method, headers, body, cookies, meta, encoding, dont_filter]) - Return a new Request which is a copy of this Request. The attribute - :attr:`Request.meta` is copied, while :attr:`Request.cache` is not. See also - :ref:`ref-request-callback-arguments`. + Return a Request object with the same members, except for those members + given new values by whichever keyword arguments are specified. The attribute + :attr:`Request.meta` is copied by default (unless a new value is given + in the ``meta`` argument). The :attr:`Request.cache` attribute is always + cleared. See also :ref:`ref-request-callback-arguments`. -.. method:: Request.replace() + .. method:: Request.httprepr() - Return a Request object with the same members, except for those members - given new values by whichever keyword arguments are specified. The attribute - :attr:`Request.meta` is copied, while :attr:`Request.cache` is not. See also - :ref:`ref-request-callback-arguments`. - -.. method:: Request.httprepr() - - Return a string with the raw HTTP representation of this response. + Return a string with the raw HTTP representation of this response. .. _ref-request-callback-copy: -Copying Requests and callbacks ------------------------------- +Caveats with copying Requests and callbacks +------------------------------------------- When you copy a request using the :meth:`Request.copy` or :meth:`Request.replace` methods the callback of the request is not copied by @@ -225,31 +221,96 @@ Using Request.meta:: Request subclasses ================== -Here is the list of built-in Request subclasses. You can also subclass the -Request class to implement your own functionality. +Here is the list of built-in :class:`Request` subclasses. You can also subclass +it to implement your own custom functionality. FormRequest objects ------------------- -.. class:: FormRequest +The FormRequest class extends the base :class:`Request` with functionality for +dealing with HTML forms. It uses the `ClientForm`_ library (bundled with +Scrapy) to pre-populate form fields with form data from :class:`Response` +objects. -The FormRequest class adds a new parameter to the constructor: +.. _ClientForm: http://wwwsearch.sourceforge.net/ClientForm/ - `formdata` - a dictionary or list of (key, value) tuples (typically - containing HTML Form data) which will be urlencoded and assigned to the body - of the request. +.. class:: FormRequest(url, [formdata, ...]) -For example, if you want to simulate a HTTP Form POST in your spider which -sends a coupe of of key-values you would return a :class:`FormRequest` object -(from your spider) like this:: + The :class:`FormRequest` class adds a new argument to the constructor. The + remaining arguments are the same as for the :class:`Request` class and are + not documented here. + + ``formdata`` is a dictionary (or iterable of (key, value) tuples) containing + HTML Form data which will be url-encoded and assigned to the body of the + request. + + The :class:`FormRequest` objects support the following class method in + addition to the standard :class:`Request` methods: + + .. classmethod:: FormRequest.from_response(response, [formnumber, formdata, ...]) + + Returns a new :class:`FormRequest` object with its form field values + pre-populated with those found in the HTML ``