Scrapy - How to keep track of start url
<p>Given a pool of start urls I would like to identify in the parse_item() function the origin url.</p>

<p>As far as I'm concerned the scrapy spiders start crawling from the initial pool of start urls, but when parsing there is no trace of which of those urls was the initial one. How it would be possible to keep track of the starting point?</p>

<p>If you need a parsing url inside the spider, just use response.url:</p>

<pre><code>def parse_item(self, response):
    print response.url 
</code></pre>

<p>but in case you need it outside spider I can think of following ways:</p>

<ol>
<li>Use <a href="http://doc.scrapy.org/en/latest/topics/api.html" rel="nofollow">scrapy core api</a></li>
<li>You can also call scrapy from an external python module with OS command (which apparently is not recommended):</li>
</ol>

<p>in scrapycaller.py</p>

<pre><code>from subprocess import call
urls = 'url1,url2'
cmd = 'scrapy crawl myspider -a myurls={}'.format(urls)
call(cmd, shell=True)
</code></pre>

<p>Inside myspider:</p>

<pre><code>class mySpider(scrapy.Spider):
    def __init__(self, myurls=''):              
        self.start_urls = myurls.split(",") 
</code></pre>

python scrapy web-crawler