Scrapy - NameError: global name 'base_search_url' is not defined
<p>I am trying to call a local variable from inside a Scrapy spider class but then I got <code>NameError: global name 'base_search_url' is not defined</code>.</p>

<pre><code>class MySpider(scrapy.Spider):
    name = "mine"
    allowed_domains = ["www.example.com"]

    base_url = "https://www.example.com"

    start_date = "2011-01-01"

    today = datetime.date.today().strftime("%Y-%m-%d")

    base_search_url = 'https://www.example.com/?city={}&amp;startDate={}&amp;endDate={}&amp;page=1',

    city_codes = ['on', 'bc', 'ab']

    start_urls = (base_search_url.format(city_code, start_date, today) for city_code in city_codes)
</code></pre>

<p>I tried to use <code>self.base_search_url</code> instead but there is no use. Does anyone know how to solve it?</p>

<p>FYI, I use Python 2.7</p>

<p>From the <a href="http://doc.scrapy.org/en/latest/topics/spiders.html#scrapy.spiders.Spider.start_urls" rel="nofollow">docs</a>: </p>

<blockquote>
  <p>start_urls: a list of URLs where the Spider will begin to crawl from.
  The first pages downloaded will be those listed here. The subsequent
  URLs will be generated successively from data contained in the start
  URLs.</p>
</blockquote>

<p>Start urls is a <strong>list</strong></p>

<p>Solve it by set in <strong>init</strong> method:</p>

<pre><code>def __init__(self):
    self.start_urls=[]
    self.start_urls.append( (base_search_url.format(city_code, start_date,     today) for city_code in city_codes) )
</code></pre>

<p>Or in the class declaration (as you show in your question):</p>

<pre><code>start_urls=[]
start_urls.append( (base_search_url.format(city_code, start_date,     today) for city_code in city_codes) )
</code></pre>

<p><strong>Note</strong>  </p>

<p>Make sure you add correct urls starting by <code>http://</code> or <code>https://</code>.</p>

python python-2.7 scrapy scrapy-spider local-variables