How to get request object in scrapy pipeline
<p>I know that when the pipelines are called, it means the request have been stopped, generally we should do some validation,persist job based on the extracted item, it seems there is no sense to get request in the pipeline.</p>

<p>However I found it may useful in certain situation,in my application I use two pipelines: <code>FilesPipeline</code> and <code>MysqlStorePipeline</code>.</p>

<p>When an item is extracted, the <code>FilesPipeline</code> will tried to send request to get the image of the item, and save them to the db after completed.</p>

<p>However I use a download middleware <code>RandomProxy</code> at the sametime, which will get a proxy record randomly from the database, and set it to the request meta. But the proxy is not granted can be used all the time.</p>

<p>So the following may happen:</p>

<p>When retrieve the item, a proxy <code>http://proxy1</code> is used, but it can not be used, thanks to the retry middleware, scrapy will try again, and another proxy <code>http://proxy2</code> is fetched from db, if it can be used, an item is generated, then <code>FilesPipeline</code>  will tried to download the image for the item by sending an image request which will be filled with a proxy say it is <code>http://proxy3</code>, once the proxy3 can not be used, scrapy will retry too. But there are chances of getting bad proxies during all the retry. Then the item will be dropped because of no bound image fetched which MUST can not be empty.</p>

<p>Furthermore, the image request does not contain a referer which may be blocked by the server sometime.</p>

<p>So I wonder if the origin request used to extract an item can be accessed through the pipeline.</p>

<p>Is this possible or other suggestion?</p>

<p>Here are two approaches:</p>

<ol>
<li><p>Add a dummy field to the item to store whatever you want in the spider code. And later retrieve the value (and pop out the field) in the item pipeline.</p></li>
<li><p>Instead of using an item pipeline, use a <a href="http://doc.scrapy.org/en/latest/topics/spider-middleware.html" rel="nofollow">spider middleware</a>. In its <code>process_spider_output</code> method you could access both the response and the spider output.</p></li>
</ol>

python scrapy