
    3 d                         d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ  G d de          Z G d	 d
e          ZdS )z
This module implements the XMLFeedSpider which is the recommended spider to use
for scraping from an XML feed.

See documentation in docs/topics/spiders.rst
    )NotConfiguredNotSupported)Selector)Spider)csviterxmliter)iterate_spider_outputc                   H    e Zd ZdZdZdZdZd Zd Zd Z	d Z
d	 Zd
 Zd ZdS )XMLFeedSpidera0  
    This class intends to be the base class for spiders that scrape
    from XML feeds.

    You can choose whether to parse the file using the 'iternodes' iterator, an
    'xml' selector, or an 'html' selector.  In most cases, it's convenient to
    use iternodes, since it's a faster and cleaner.
    	iternodesitem c                     |S )a  This overridable method is called for each result (item or request)
        returned by the spider, and it's intended to perform any last time
        processing required before returning the results to the framework core,
        for example setting the item GUIDs. It receives a list of results and
        the response which originated that results. It must return a list of
        results (items or requests).
        r   selfresponseresultss      3lib/python3.11/site-packages/scrapy/spiders/feed.pyprocess_resultszXMLFeedSpider.process_results   s	         c                     |S )zYou can override this function in order to make any changes you want
        to into the feed before parsing it. This function must return a
        response.
        r   r   r   s     r   adapt_responsezXMLFeedSpider.adapt_response&   s	    
 r   c                 \    t          | d          r|                     ||          S t          )DThis method must be overridden with your custom spider functionality
parse_item)hasattrr   NotImplementedError)r   r   selectors      r   
parse_nodezXMLFeedSpider.parse_node-   s.    4&& 	7??8X666!!r   c              #      K   |D ]B}t          |                     ||                    }|                     ||          D ]}|V  CdS )aG  This method is called for the nodes matching the provided tag name
        (itertag). Receives the response and an Selector for each node.
        Overriding this method is mandatory. Otherwise, you spider won't work.
        This method must return either an item, a request, or a list
        containing any of them.
        N)r	   r    r   )r   r   nodesr   retresult_items         r   parse_nodeszXMLFeedSpider.parse_nodes3   so        	" 	"H'((K(KLLC#33HcBB " "!!!!!"	" 	"r   c                 2   t          | d          st          d          |                     |          }| j        dk    r|                     |          }n| j        dk    rDt          |d          }|                     |           |                    d| j                   }n^| j        dk    rDt          |d          }|                     |           |                    d| j                   }nt          d          | 
                    ||          S )	Nr    zBYou must define parse_node method in order to scrape this XML feedr   xml)typez//htmlzUnsupported node iterator)r   r   r   iterator
_iternodesr   _register_namespacesxpathitertagr   r%   )r   r   kwargsr"   r   s        r   _parsezXMLFeedSpider._parse@   s+   t\** 	T   &&x00=K' 	<OOH--EE]e# 		<u555H%%h///NN#6#6#677EE]f$ 	<v666H%%h///NN#6#6#677EE:;;;%000r   c              #   l   K   t          || j                  D ]}|                     |           |V  d S N)r   r.   r,   )r   r   nodes      r   r+   zXMLFeedSpider._iternodesV   sJ      Hdl33 	 	D%%d+++JJJJ	 	r   c                 L    | j         D ]\  }}|                    ||           d S r2   )
namespacesregister_namespace)r   r   prefixuris       r   r,   z"XMLFeedSpider._register_namespaces[   s:    !_ 	5 	5MVS''4444	5 	5r   N)__name__
__module____qualname____doc__r*   r.   r5   r   r   r    r%   r0   r+   r,   r   r   r   r   r      s          HGJ    " " "" " "1 1 1,  
5 5 5 5 5r   r   c                   <    e Zd ZdZdZdZdZd Zd Zd Z	d Z
d ZdS )CSVFeedSpidera$  Spider for parsing CSV feeds.
    It receives a CSV file in a response; iterates through each of its rows,
    and calls parse_row with a dict containing each field's data.

    You can set some options regarding the CSV file, such as the delimiter, quotechar
    and the file's headers.
    Nc                     |S z<This method has the same purpose as the one in XMLFeedSpiderr   r   s      r   r   zCSVFeedSpider.process_resultsq   s    r   c                     |S r@   r   r   s     r   r   zCSVFeedSpider.adapt_responseu   s    r   c                     t           )r   )r   )r   r   rows      r   	parse_rowzCSVFeedSpider.parse_rowy   s    !!r   c              #      K   t          || j        | j        | j                  D ]B}t	          |                     ||                    }|                     ||          D ]}|V  CdS )a  Receives a response and a dict (representing each row) with a key for
        each provided (or detected) header of the CSV file.  This spider also
        gives the opportunity to override adapt_response and
        process_results methods for pre and post-processing purposes.
        )	quotecharN)r   	delimiterheadersrF   r	   rD   r   )r   r   rC   r#   r$   s        r   
parse_rowszCSVFeedSpider.parse_rows}   s       dndldn
 
 
 	" 	"C (x(E(EFFC#33HcBB " "!!!!!"		" 	"r   c                     t          | d          st          d          |                     |          }|                     |          S )NrD   zAYou must define parse_row method in order to scrape this CSV feed)r   r   r   rI   )r   r   r/   s      r   r0   zCSVFeedSpider._parse   sP    t[)) 	S   &&x00x(((r   )r9   r:   r;   r<   rG   rF   rH   r   r   rD   rI   r0   r   r   r   r>   r>   `   s          	  	  G    " " "" " ") ) ) ) )r   r>   N)r<   scrapy.exceptionsr   r   scrapy.selectorr   scrapy.spidersr   scrapy.utils.iteratorsr   r   scrapy.utils.spiderr	   r   r>   r   r   r   <module>rP      s     : 9 9 9 9 9 9 9 $ $ $ $ $ $ ! ! ! ! ! ! 3 3 3 3 3 3 3 3 5 5 5 5 5 5O5 O5 O5 O5 O5F O5 O5 O5d1) 1) 1) 1) 1)F 1) 1) 1) 1) 1)r   