I know this is a newbie question, and this is the main Python question, but it is in the context of Scrapy and I cannot find the answer anywhere.
When I run this bot code:
import scrapy from tutorial.items import DmozItem class DmozSpider(scrapy.Spider): name = "dmoz" allowed_domains = ["lib-web.org"] start_urls = [ "http://www.lib-web.org/united-states/public-libraries/michigan/" ] count = 0 def increment(self): global count count += 1 def getCount(self): global count return count def parse(self, response): increment() for sel in response.xpath('//div/div/div/ul/li'): item = DmozItem() item['title'] = sel.xpath('a/text()').extract() item['link'] = sel.xpath('a/@href').extract() item['desc'] = sel.xpath('p/text()').extract() x = getCount() print x yield item
DmozItem:
import scrapy class DmozItem(scrapy.Item): title = scrapy.Field() link = scrapy.Field() desc = scrapy.Field()
I get this error:
File "/Users/Admin/scpy_projs/tutorial/tutorial/spiders/dmoz_spider.py", line 23, in parse increment() NameError: global name 'increment' is not defined
Why can't I call increment() from parse(self, response) ? How can I do this job?
Thanks for any help.
source share