How to determine a bug in a Scrapy item loader implementation?
up vote
0
down vote
favorite
I'm attempting a scraping project with the Scrapy framework that requires (I think) that I use item loaders for some of the processing. After getting nowhere I decided to try to refactor the Scrapy tutorial quotes project to narrow down my issue and I am getting the same errors. I'm fairly certain that I'm making an error with the item loaders on the actual project and here.
items.py:
def clean_text(value):
return value.strip()
class QuoteItem(Item):
text = scrapy.Field()
author = scrapy.Field()
tags = scrapy.Field()
class QuoteLoader(ItemLoader):
default_input_processor = MapCompose(clean_text)
default_output_processor = TakeFirst()
text_in = MapCompose(clean_text)
author_in = MapCompose(clean_text)
tags_in = MapCompose(clean_text)
quotes_spider(refactored and broken):
class QuotesSpider1(scrapy.Spider):
name = "quotes1"
start_urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
def parse(self, response):
for quote in response.css('div.quote'):
load = QuoteLoader(item=QuoteItem(), selector=quote)
load.add_css('text', 'span.text::text').extract_first()
load.add_css('author', 'small.author::text').extract_first()
load.add_css('tags', 'div.tags a.tag::text').extract()
yield load.load_item()
quotes_spider from tutorial(functional):
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').extract_first(),
'author': quote.css('small.author::text').extract_first(),
'tags': quote.css('div.tags a.tag::text').extract(),
}
traceback and log:
2018-11-04 21:12:24 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: tutorial)
2018-11-04 21:12:24 [scrapy.utils.log] INFO: Versions: lxml 4.2.5.0, libxml2 2.9.8, cssselect 1.0.3, parsel 1.5.1, w3lib 1.19.0, Twisted 18.9.0, Python 3.6.7 |Anaconda,
Inc.| (default, Oct 28 2018, 19:44:12) [MSC v.1915 64 bit (AMD64)], pyOpenSSL 18.0.0 (OpenSSL 1.1.1 11 Sep 2018), cryptography 2.3.1, Platform Windows-10-10.0.17134-S
P0
2018-11-04 21:12:24 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'tutorial', 'NEWSPIDER_MODULE': 'tutorial.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES
': ['tutorial.spiders']}
2018-11-04 21:12:25 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2018-11-04 21:12:26 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2018-11-04 21:12:26 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2018-11-04 21:12:26 [scrapy.middleware] INFO: Enabled item pipelines:
2018-11-04 21:12:26 [scrapy.core.engine] INFO: Spider opened
2018-11-04 21:12:26 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2018-11-04 21:12:26 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6024
2018-11-04 21:12:27 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.jdpower.com/robots.txt> (referer: None)
2018-11-04 21:12:28 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.jdpower.com/Cars/2019/Chevrolet> (referer: None)
2018-11-04 21:12:28 [scrapy.core.scraper] ERROR: Spider error processing <GET https://www.jdpower.com/Cars/2019/Chevrolet> (referer: None)
Traceback (most recent call last):
File "C:UsersMEAnaconda3envstutoriallibsite-packagestwistedinternetdefer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapycommandsparse.py", line 195, in callback
items, requests = self.run_callback(response, cb)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapycommandsparse.py", line 117, in run_callback
for x in iterate_spider_output(cb(response)):
File "C:UsersMEPycharmProjectstutorialtutorialspidersmsrp_trial_spider.py", line 38, in parse
load.get_xpath('full_name', '//*[contains(@class,"veh-icons__title")]/text()').extract()
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyloader__init__.py", line 175, in get_xpath
return self.get_value(values, *processors, **kw)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyloader__init__.py", line 108, in get_value
proc = wrap_loader_context(proc, self.context)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyloadercommon.py", line 10, in wrap_loader_context
if 'loader_context' in get_func_args(function):
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyutilspython.py", line 241, in get_func_args
raise TypeError('%s is not callable' % type(func))
TypeError: <class 'str'> is not callable
2018-11-04 21:12:28 [scrapy.core.engine] INFO: Closing spider (finished)
2018-11-04 21:12:28 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 519,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 22637,
'downloader/response_count': 2,
'downloader/response_status_count/200': 2,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2018, 11, 5, 3, 12, 28, 737277),
'log_count/DEBUG': 3,
'log_count/ERROR': 1,
'log_count/INFO': 7,
'response_received_count': 2,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'spider_exceptions/TypeError': 1,
'start_time': datetime.datetime(2018, 11, 5, 3, 12, 26, 497083)}
2018-11-04 21:12:28 [scrapy.core.engine] INFO: Spider closed (finished)
>>> STATUS DEPTH LEVEL 0 <<<
# Scraped Items ------------------------------------------------------------
# Requests -----------------------------------------------------------------
python python-3.x logging scrapy refactoring
New contributor
add a comment |
up vote
0
down vote
favorite
I'm attempting a scraping project with the Scrapy framework that requires (I think) that I use item loaders for some of the processing. After getting nowhere I decided to try to refactor the Scrapy tutorial quotes project to narrow down my issue and I am getting the same errors. I'm fairly certain that I'm making an error with the item loaders on the actual project and here.
items.py:
def clean_text(value):
return value.strip()
class QuoteItem(Item):
text = scrapy.Field()
author = scrapy.Field()
tags = scrapy.Field()
class QuoteLoader(ItemLoader):
default_input_processor = MapCompose(clean_text)
default_output_processor = TakeFirst()
text_in = MapCompose(clean_text)
author_in = MapCompose(clean_text)
tags_in = MapCompose(clean_text)
quotes_spider(refactored and broken):
class QuotesSpider1(scrapy.Spider):
name = "quotes1"
start_urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
def parse(self, response):
for quote in response.css('div.quote'):
load = QuoteLoader(item=QuoteItem(), selector=quote)
load.add_css('text', 'span.text::text').extract_first()
load.add_css('author', 'small.author::text').extract_first()
load.add_css('tags', 'div.tags a.tag::text').extract()
yield load.load_item()
quotes_spider from tutorial(functional):
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').extract_first(),
'author': quote.css('small.author::text').extract_first(),
'tags': quote.css('div.tags a.tag::text').extract(),
}
traceback and log:
2018-11-04 21:12:24 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: tutorial)
2018-11-04 21:12:24 [scrapy.utils.log] INFO: Versions: lxml 4.2.5.0, libxml2 2.9.8, cssselect 1.0.3, parsel 1.5.1, w3lib 1.19.0, Twisted 18.9.0, Python 3.6.7 |Anaconda,
Inc.| (default, Oct 28 2018, 19:44:12) [MSC v.1915 64 bit (AMD64)], pyOpenSSL 18.0.0 (OpenSSL 1.1.1 11 Sep 2018), cryptography 2.3.1, Platform Windows-10-10.0.17134-S
P0
2018-11-04 21:12:24 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'tutorial', 'NEWSPIDER_MODULE': 'tutorial.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES
': ['tutorial.spiders']}
2018-11-04 21:12:25 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2018-11-04 21:12:26 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2018-11-04 21:12:26 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2018-11-04 21:12:26 [scrapy.middleware] INFO: Enabled item pipelines:
2018-11-04 21:12:26 [scrapy.core.engine] INFO: Spider opened
2018-11-04 21:12:26 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2018-11-04 21:12:26 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6024
2018-11-04 21:12:27 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.jdpower.com/robots.txt> (referer: None)
2018-11-04 21:12:28 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.jdpower.com/Cars/2019/Chevrolet> (referer: None)
2018-11-04 21:12:28 [scrapy.core.scraper] ERROR: Spider error processing <GET https://www.jdpower.com/Cars/2019/Chevrolet> (referer: None)
Traceback (most recent call last):
File "C:UsersMEAnaconda3envstutoriallibsite-packagestwistedinternetdefer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapycommandsparse.py", line 195, in callback
items, requests = self.run_callback(response, cb)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapycommandsparse.py", line 117, in run_callback
for x in iterate_spider_output(cb(response)):
File "C:UsersMEPycharmProjectstutorialtutorialspidersmsrp_trial_spider.py", line 38, in parse
load.get_xpath('full_name', '//*[contains(@class,"veh-icons__title")]/text()').extract()
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyloader__init__.py", line 175, in get_xpath
return self.get_value(values, *processors, **kw)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyloader__init__.py", line 108, in get_value
proc = wrap_loader_context(proc, self.context)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyloadercommon.py", line 10, in wrap_loader_context
if 'loader_context' in get_func_args(function):
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyutilspython.py", line 241, in get_func_args
raise TypeError('%s is not callable' % type(func))
TypeError: <class 'str'> is not callable
2018-11-04 21:12:28 [scrapy.core.engine] INFO: Closing spider (finished)
2018-11-04 21:12:28 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 519,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 22637,
'downloader/response_count': 2,
'downloader/response_status_count/200': 2,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2018, 11, 5, 3, 12, 28, 737277),
'log_count/DEBUG': 3,
'log_count/ERROR': 1,
'log_count/INFO': 7,
'response_received_count': 2,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'spider_exceptions/TypeError': 1,
'start_time': datetime.datetime(2018, 11, 5, 3, 12, 26, 497083)}
2018-11-04 21:12:28 [scrapy.core.engine] INFO: Spider closed (finished)
>>> STATUS DEPTH LEVEL 0 <<<
# Scraped Items ------------------------------------------------------------
# Requests -----------------------------------------------------------------
python python-3.x logging scrapy refactoring
New contributor
1
It would be much easier to help if your code matched the log.
– stranac
Nov 5 at 6:19
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm attempting a scraping project with the Scrapy framework that requires (I think) that I use item loaders for some of the processing. After getting nowhere I decided to try to refactor the Scrapy tutorial quotes project to narrow down my issue and I am getting the same errors. I'm fairly certain that I'm making an error with the item loaders on the actual project and here.
items.py:
def clean_text(value):
return value.strip()
class QuoteItem(Item):
text = scrapy.Field()
author = scrapy.Field()
tags = scrapy.Field()
class QuoteLoader(ItemLoader):
default_input_processor = MapCompose(clean_text)
default_output_processor = TakeFirst()
text_in = MapCompose(clean_text)
author_in = MapCompose(clean_text)
tags_in = MapCompose(clean_text)
quotes_spider(refactored and broken):
class QuotesSpider1(scrapy.Spider):
name = "quotes1"
start_urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
def parse(self, response):
for quote in response.css('div.quote'):
load = QuoteLoader(item=QuoteItem(), selector=quote)
load.add_css('text', 'span.text::text').extract_first()
load.add_css('author', 'small.author::text').extract_first()
load.add_css('tags', 'div.tags a.tag::text').extract()
yield load.load_item()
quotes_spider from tutorial(functional):
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').extract_first(),
'author': quote.css('small.author::text').extract_first(),
'tags': quote.css('div.tags a.tag::text').extract(),
}
traceback and log:
2018-11-04 21:12:24 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: tutorial)
2018-11-04 21:12:24 [scrapy.utils.log] INFO: Versions: lxml 4.2.5.0, libxml2 2.9.8, cssselect 1.0.3, parsel 1.5.1, w3lib 1.19.0, Twisted 18.9.0, Python 3.6.7 |Anaconda,
Inc.| (default, Oct 28 2018, 19:44:12) [MSC v.1915 64 bit (AMD64)], pyOpenSSL 18.0.0 (OpenSSL 1.1.1 11 Sep 2018), cryptography 2.3.1, Platform Windows-10-10.0.17134-S
P0
2018-11-04 21:12:24 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'tutorial', 'NEWSPIDER_MODULE': 'tutorial.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES
': ['tutorial.spiders']}
2018-11-04 21:12:25 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2018-11-04 21:12:26 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2018-11-04 21:12:26 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2018-11-04 21:12:26 [scrapy.middleware] INFO: Enabled item pipelines:
2018-11-04 21:12:26 [scrapy.core.engine] INFO: Spider opened
2018-11-04 21:12:26 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2018-11-04 21:12:26 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6024
2018-11-04 21:12:27 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.jdpower.com/robots.txt> (referer: None)
2018-11-04 21:12:28 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.jdpower.com/Cars/2019/Chevrolet> (referer: None)
2018-11-04 21:12:28 [scrapy.core.scraper] ERROR: Spider error processing <GET https://www.jdpower.com/Cars/2019/Chevrolet> (referer: None)
Traceback (most recent call last):
File "C:UsersMEAnaconda3envstutoriallibsite-packagestwistedinternetdefer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapycommandsparse.py", line 195, in callback
items, requests = self.run_callback(response, cb)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapycommandsparse.py", line 117, in run_callback
for x in iterate_spider_output(cb(response)):
File "C:UsersMEPycharmProjectstutorialtutorialspidersmsrp_trial_spider.py", line 38, in parse
load.get_xpath('full_name', '//*[contains(@class,"veh-icons__title")]/text()').extract()
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyloader__init__.py", line 175, in get_xpath
return self.get_value(values, *processors, **kw)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyloader__init__.py", line 108, in get_value
proc = wrap_loader_context(proc, self.context)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyloadercommon.py", line 10, in wrap_loader_context
if 'loader_context' in get_func_args(function):
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyutilspython.py", line 241, in get_func_args
raise TypeError('%s is not callable' % type(func))
TypeError: <class 'str'> is not callable
2018-11-04 21:12:28 [scrapy.core.engine] INFO: Closing spider (finished)
2018-11-04 21:12:28 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 519,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 22637,
'downloader/response_count': 2,
'downloader/response_status_count/200': 2,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2018, 11, 5, 3, 12, 28, 737277),
'log_count/DEBUG': 3,
'log_count/ERROR': 1,
'log_count/INFO': 7,
'response_received_count': 2,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'spider_exceptions/TypeError': 1,
'start_time': datetime.datetime(2018, 11, 5, 3, 12, 26, 497083)}
2018-11-04 21:12:28 [scrapy.core.engine] INFO: Spider closed (finished)
>>> STATUS DEPTH LEVEL 0 <<<
# Scraped Items ------------------------------------------------------------
# Requests -----------------------------------------------------------------
python python-3.x logging scrapy refactoring
New contributor
I'm attempting a scraping project with the Scrapy framework that requires (I think) that I use item loaders for some of the processing. After getting nowhere I decided to try to refactor the Scrapy tutorial quotes project to narrow down my issue and I am getting the same errors. I'm fairly certain that I'm making an error with the item loaders on the actual project and here.
items.py:
def clean_text(value):
return value.strip()
class QuoteItem(Item):
text = scrapy.Field()
author = scrapy.Field()
tags = scrapy.Field()
class QuoteLoader(ItemLoader):
default_input_processor = MapCompose(clean_text)
default_output_processor = TakeFirst()
text_in = MapCompose(clean_text)
author_in = MapCompose(clean_text)
tags_in = MapCompose(clean_text)
quotes_spider(refactored and broken):
class QuotesSpider1(scrapy.Spider):
name = "quotes1"
start_urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
def parse(self, response):
for quote in response.css('div.quote'):
load = QuoteLoader(item=QuoteItem(), selector=quote)
load.add_css('text', 'span.text::text').extract_first()
load.add_css('author', 'small.author::text').extract_first()
load.add_css('tags', 'div.tags a.tag::text').extract()
yield load.load_item()
quotes_spider from tutorial(functional):
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').extract_first(),
'author': quote.css('small.author::text').extract_first(),
'tags': quote.css('div.tags a.tag::text').extract(),
}
traceback and log:
2018-11-04 21:12:24 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: tutorial)
2018-11-04 21:12:24 [scrapy.utils.log] INFO: Versions: lxml 4.2.5.0, libxml2 2.9.8, cssselect 1.0.3, parsel 1.5.1, w3lib 1.19.0, Twisted 18.9.0, Python 3.6.7 |Anaconda,
Inc.| (default, Oct 28 2018, 19:44:12) [MSC v.1915 64 bit (AMD64)], pyOpenSSL 18.0.0 (OpenSSL 1.1.1 11 Sep 2018), cryptography 2.3.1, Platform Windows-10-10.0.17134-S
P0
2018-11-04 21:12:24 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'tutorial', 'NEWSPIDER_MODULE': 'tutorial.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES
': ['tutorial.spiders']}
2018-11-04 21:12:25 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2018-11-04 21:12:26 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2018-11-04 21:12:26 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2018-11-04 21:12:26 [scrapy.middleware] INFO: Enabled item pipelines:
2018-11-04 21:12:26 [scrapy.core.engine] INFO: Spider opened
2018-11-04 21:12:26 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2018-11-04 21:12:26 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6024
2018-11-04 21:12:27 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.jdpower.com/robots.txt> (referer: None)
2018-11-04 21:12:28 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.jdpower.com/Cars/2019/Chevrolet> (referer: None)
2018-11-04 21:12:28 [scrapy.core.scraper] ERROR: Spider error processing <GET https://www.jdpower.com/Cars/2019/Chevrolet> (referer: None)
Traceback (most recent call last):
File "C:UsersMEAnaconda3envstutoriallibsite-packagestwistedinternetdefer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapycommandsparse.py", line 195, in callback
items, requests = self.run_callback(response, cb)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapycommandsparse.py", line 117, in run_callback
for x in iterate_spider_output(cb(response)):
File "C:UsersMEPycharmProjectstutorialtutorialspidersmsrp_trial_spider.py", line 38, in parse
load.get_xpath('full_name', '//*[contains(@class,"veh-icons__title")]/text()').extract()
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyloader__init__.py", line 175, in get_xpath
return self.get_value(values, *processors, **kw)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyloader__init__.py", line 108, in get_value
proc = wrap_loader_context(proc, self.context)
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyloadercommon.py", line 10, in wrap_loader_context
if 'loader_context' in get_func_args(function):
File "C:UsersMEAnaconda3envstutoriallibsite-packagesscrapyutilspython.py", line 241, in get_func_args
raise TypeError('%s is not callable' % type(func))
TypeError: <class 'str'> is not callable
2018-11-04 21:12:28 [scrapy.core.engine] INFO: Closing spider (finished)
2018-11-04 21:12:28 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 519,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 22637,
'downloader/response_count': 2,
'downloader/response_status_count/200': 2,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2018, 11, 5, 3, 12, 28, 737277),
'log_count/DEBUG': 3,
'log_count/ERROR': 1,
'log_count/INFO': 7,
'response_received_count': 2,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'spider_exceptions/TypeError': 1,
'start_time': datetime.datetime(2018, 11, 5, 3, 12, 26, 497083)}
2018-11-04 21:12:28 [scrapy.core.engine] INFO: Spider closed (finished)
>>> STATUS DEPTH LEVEL 0 <<<
# Scraped Items ------------------------------------------------------------
# Requests -----------------------------------------------------------------
python python-3.x logging scrapy refactoring
python python-3.x logging scrapy refactoring
New contributor
New contributor
edited Nov 5 at 7:44
halfer
14.1k757104
14.1k757104
New contributor
asked Nov 5 at 3:40
dudeguy
1
1
New contributor
New contributor
1
It would be much easier to help if your code matched the log.
– stranac
Nov 5 at 6:19
add a comment |
1
It would be much easier to help if your code matched the log.
– stranac
Nov 5 at 6:19
1
1
It would be much easier to help if your code matched the log.
– stranac
Nov 5 at 6:19
It would be much easier to help if your code matched the log.
– stranac
Nov 5 at 6:19
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
The problem is that you're calling .extract_first()
on the result of the add_css
method, which returns None
.
Removing such calls should be enough:
def parse(self, response):
for quote in response.css('div.quote'):
load = QuoteLoader(item=QuoteItem(), selector=quote)
load.add_css('text', 'span.text::text')
load.add_css('author', 'small.author::text')
load.add_css('tags', 'div.tags a.tag::text')
yield load.load_item()
You'll probably need to customize the output processor for the tags
field, so that you don't use the TakeFirst()
one, which will result in a single tag per item, which is not true for this website.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The problem is that you're calling .extract_first()
on the result of the add_css
method, which returns None
.
Removing such calls should be enough:
def parse(self, response):
for quote in response.css('div.quote'):
load = QuoteLoader(item=QuoteItem(), selector=quote)
load.add_css('text', 'span.text::text')
load.add_css('author', 'small.author::text')
load.add_css('tags', 'div.tags a.tag::text')
yield load.load_item()
You'll probably need to customize the output processor for the tags
field, so that you don't use the TakeFirst()
one, which will result in a single tag per item, which is not true for this website.
add a comment |
up vote
0
down vote
The problem is that you're calling .extract_first()
on the result of the add_css
method, which returns None
.
Removing such calls should be enough:
def parse(self, response):
for quote in response.css('div.quote'):
load = QuoteLoader(item=QuoteItem(), selector=quote)
load.add_css('text', 'span.text::text')
load.add_css('author', 'small.author::text')
load.add_css('tags', 'div.tags a.tag::text')
yield load.load_item()
You'll probably need to customize the output processor for the tags
field, so that you don't use the TakeFirst()
one, which will result in a single tag per item, which is not true for this website.
add a comment |
up vote
0
down vote
up vote
0
down vote
The problem is that you're calling .extract_first()
on the result of the add_css
method, which returns None
.
Removing such calls should be enough:
def parse(self, response):
for quote in response.css('div.quote'):
load = QuoteLoader(item=QuoteItem(), selector=quote)
load.add_css('text', 'span.text::text')
load.add_css('author', 'small.author::text')
load.add_css('tags', 'div.tags a.tag::text')
yield load.load_item()
You'll probably need to customize the output processor for the tags
field, so that you don't use the TakeFirst()
one, which will result in a single tag per item, which is not true for this website.
The problem is that you're calling .extract_first()
on the result of the add_css
method, which returns None
.
Removing such calls should be enough:
def parse(self, response):
for quote in response.css('div.quote'):
load = QuoteLoader(item=QuoteItem(), selector=quote)
load.add_css('text', 'span.text::text')
load.add_css('author', 'small.author::text')
load.add_css('tags', 'div.tags a.tag::text')
yield load.load_item()
You'll probably need to customize the output processor for the tags
field, so that you don't use the TakeFirst()
one, which will result in a single tag per item, which is not true for this website.
answered Nov 5 at 16:42
Valdir Stumm Junior
2,9651524
2,9651524
add a comment |
add a comment |
dudeguy is a new contributor. Be nice, and check out our Code of Conduct.
dudeguy is a new contributor. Be nice, and check out our Code of Conduct.
dudeguy is a new contributor. Be nice, and check out our Code of Conduct.
dudeguy is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148006%2fhow-to-determine-a-bug-in-a-scrapy-item-loader-implementation%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
It would be much easier to help if your code matched the log.
– stranac
Nov 5 at 6:19