scrapy meta={"item": item} 重载时报错 KeyError: 'item'
*若价格不公道,可以让提问者在平台追加赏金哦,平台是您利益的保证
待解决
问题详情:分类: python
import scrapy
from tietu51.items import Tietu51Item
import copy
from scrapy import Request
#from ..items import TietuItem
class Spider(scrapy.Spider):
name = "tietu51"
allowed_domains = ["51tietu.net"]
start_urls = (
['https://www.51tietu.net/pic/']
)
def parse_herf(self, response):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
url = response.xpath('.//div[@class="box-img"]/a/@href').extract()
for li in url:
item = Tietu51Item()
targs= 'https://www.51tietu.net' + li
item['targs'] = targs
yield scrapy.Request(url=targs, callback=self.parse, meta={'item': item})
#yield scrapy.Request(url=item['targs'], meta={"item": item}, callback=self.parse,headers=headers, dont_filter=True)
def parse(self, response):
item = response.meta["item"]
page_lists = response.xpath('//*[@id="pic"]/div[1]/p/img/@src').extract()
item['image_urls'] = page_lists
yield item
D:\untitled4\venv\Scripts\python.exe D:/scrapy/tietu51/tietu51/begin.py
2021-11-13 19:00:16 [scrapy.utils.log] INFO: Scrapy 2.5.1 started (bot: tietu51)
2021-11-13 19:00:16 [scrapy.utils.log] INFO: Versions: lxml 4.6.3.0, libxml2 2.9.5, cssselect 1.1.0, parsel 1.6.0, w3lib 1.22.0, Twisted 21.7.0, Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)], pyOpenSSL 21.0.0 (OpenSSL 1.1.1l 24 Aug 2021), cryptography 35.0.0, Platform Windows-10-10.0.17763-SP0
2021-11-13 19:00:16 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.selectreactor.SelectReactor
2021-11-13 19:00:16 [scrapy.crawler] INFO: Overridden settings:
{'BOT_NAME': 'tietu51',
'NEWSPIDER_MODULE': 'tietu51.spiders',
'SPIDER_MODULES': ['tietu51.spiders']}
2021-11-13 19:00:16 [scrapy.extensions.telnet] INFO: Telnet Password: 7c5733e91587c901
2021-11-13 19:00:16 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.logstats.LogStats']
2021-11-13 19:00:16 [scrapy.middleware] INFO: Enabled downloader middlewares:
['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']
2021-11-13 19:00:16 [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']
2021-11-13 19:00:16 [scrapy.middleware] INFO: Enabled item pipelines:
['tietu51.pipelines.Getlixiaoranpipeline']
2021-11-13 19:00:16 [scrapy.core.engine] INFO: Spider opened
2021-11-13 19:00:16 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2021-11-13 19:00:16 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2021-11-13 19:00:16 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (301) to <GET https://www.51tietu.net/pic/%E9%BB%91%E4%B8%9D%E7%BE%8E%E8%85%BF/> from <GET https://www.51tietu.net/pic/%E9%BB%91%E4%B8%9D%E7%BE%8E%E8%85%BF>
2021-11-13 19:00:17 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.51tietu.net/pic/%E9%BB%91%E4%B8%9D%E7%BE%8E%E8%85%BF/> (referer: None)
2021-11-13 19:00:17 [scrapy.core.scraper] ERROR: Spider error processing <GET https://www.51tietu.net/pic/%E9%BB%91%E4%B8%9D%E7%BE%8E%E8%85%BF/> (referer: None)
Traceback (most recent call last):
File "D:\untitled4\venv\lib\site-packages\scrapy\utils\defer.py", line 120, in iter_errback
yield next(it)
File "D:\untitled4\venv\lib\site-packages\scrapy\utils\python.py", line 353, in __next__
return next(self.data)
File "D:\untitled4\venv\lib\site-packages\scrapy\utils\python.py", line 353, in __next__
return next(self.data)
File "D:\untitled4\venv\lib\site-packages\scrapy\core\spidermw.py", line 56, in _evaluate_iterable
for r in iterable:
File "D:\untitled4\venv\lib\site-packages\scrapy\spidermiddlewares\offsite.py", line 29, in process_spider_output
for x in result:
File "D:\untitled4\venv\lib\site-packages\scrapy\core\spidermw.py", line 56, in _evaluate_iterable
for r in iterable:
File "D:\untitled4\venv\lib\site-packages\scrapy\spidermiddlewares\referer.py", line 342, in <genexpr>
return (_set_referer(r) for r in result or ())
File "D:\untitled4\venv\lib\site-packages\scrapy\core\spidermw.py", line 56, in _evaluate_iterable
for r in iterable:
File "D:\untitled4\venv\lib\site-packages\scrapy\spidermiddlewares\urllength.py", line 40, in <genexpr>
return (r for r in result or () if _filter(r))
File "D:\untitled4\venv\lib\site-packages\scrapy\core\spidermw.py", line 56, in _evaluate_iterable
for r in iterable:
File "D:\untitled4\venv\lib\site-packages\scrapy\spidermiddlewares\depth.py", line 58, in <genexpr>
return (r for r in result or () if _filter(r))
File "D:\untitled4\venv\lib\site-packages\scrapy\core\spidermw.py", line 56, in _evaluate_iterable
for r in iterable:
File "D:\scrapy\tietu51\tietu51\spiders\spider.py", line 33, in parse
item = response.meta["item"]
KeyError: 'item'
2021-11-13 19:00:17 [scrapy.core.engine] INFO: Closing spider (finished)
2021-11-13 19:00:17 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 511,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 61409,
'downloader/response_count': 2,
'downloader/response_status_count/200': 1,
'downloader/response_status_count/301': 1,
'elapsed_time_seconds': 0.806802,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2021, 11, 13, 11, 0, 17, 209827),
'httpcompression/response_bytes': 232077,
'httpcompression/response_count': 1,
'log_count/DEBUG': 2,
'log_count/ERROR': 1,
'log_count/INFO': 10,
'response_received_count': 1,
'scheduler/dequeued': 2,
'scheduler/dequeued/memory': 2,
'scheduler/enqueued': 2,
'scheduler/enqueued/memory': 2,
'spider_exceptions/KeyError': 1,
'start_time': datetime.datetime(2021, 11, 13, 11, 0, 16, 403025)}
2021-11-13 19:00:17 [scrapy.core.engine] INFO: Spider closed (finished)
Process finished with exit code 0
*若价格不公道,可以让提问者在平台追加赏金哦,平台是您利益的保证。你觉得当前的价格如何呢,奉上您珍贵的一票吧
虚高0人次 适中0人次 偏低0人次
分享海报会更快解决你的问题哦!分享海报
此处可发布评论
评论(0)
暂无评论,快来写一下吧