属性错误:'NoneType'对象没有'strip'属性- Scrapy不会编目所有项目

2wnc66cl  于 2022-11-23  发布在  其他
关注(0)|答案(2)|浏览(130)

我的蜘蛛不爬所有的元素。
我现在看到的其中一个错误是属性错误,我不知道如何修复它。这是一个非英文网站,我想把它的数字变成英文的,没有“%”和“,"。当我问的时候,我被建议使用strip()和replace()。但是现在,问题是AttributeError:'NoneType'对象没有'strip'属性
这是我得到的日志:

2020-09-01 16:23:53 [scrapy.utils.log] INFO: Scrapy 1.6.0 started (bot: digikala)
2020-09-01 16:23:53 [scrapy.utils.log] INFO: Versions: lxml 4.5.2.0, libxml2 2.9.10, cssselect 1.1.0, parsel 1.5.2, w3lib 1.21.0, Twisted 20.3.0, Python 3.8.5 (default, Aug  5 2020, 09:44:06) [MSC v.1916 64 bit (AMD64)], pyOpenSSL 19.1.0 (OpenSSL 1.1.1g  21 Apr 2020), cryptography 2.9.2, Platform Windows-10-10.0.19041-SP0
2020-09-01 16:23:53 [scrapy.crawler] INFO: Overridden settings: {'AUTOTHROTTLE_MAX_DELAY': 120, 'AUTOTHROTTLE_START_DELAY': 60, 'BOT_NAME': 'digikala', 'FEED_FORMAT': 'xml', 'FEED_URI': '99-6-11-12.xml', 'NEWSPIDER_MODULE': 'digikala.spiders', 'SPIDER_MODULES': ['digikala.spiders']}
2020-09-01 16:23:53 [scrapy.extensions.telnet] INFO: Telnet Password: 87dd5cd5e47f9a7a
2020-09-01 16:23:53 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.feedexport.FeedExporter',
 'scrapy.extensions.logstats.LogStats']
2020-09-01 16:23:53 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'digikala.middlewares.UserAgentRotatorMiddleware',
 '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']
2020-09-01 16:23:53 [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']
2020-09-01 16:23:53 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2020-09-01 16:23:53 [scrapy.core.engine] INFO: Spider opened
2020-09-01 16:23:53 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2020-09-01 16:23:53 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-09-01 16:23:54 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.digikala.com/search/category-book/?type[0]=4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4> (referer: None)
2020-09-01 16:23:54 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.digikala.com/search/category-book/?type[0]=4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4>
{'title': 'کتاب خودت را به فنا نده اثر گری جان بیشاپ', 'star': 4.4, 'discounted_percent': 28, 'discounted_price': 13000, 'original_price': 18000, 'discounted_amount': 5000, 'url': 'https://www.digikala.com/product/dkp-1050124/کتاب-خودت-را-به-فنا-نده-اثر-گری-جان-بیشاپ'}
2020-09-01 16:23:54 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.digikala.com/search/category-book/?type[0]=4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4>
{'title': 'کتاب شرمنده نباش دختر اثر ریچل هالیس نشر کتاب کوله پشتی', 'star': 4.6, 'discounted_percent': 15, 'discounted_price': 25500, 'original_price': 30000, 'discounted_amount': 4500, 'url': 'https://www.digikala.com/product/dkp-1556940/کتاب-شرمنده-نباش-دختر-اثر-ریچل-هالیس-نشر-کتاب-کوله-پشتی'}
2020-09-01 16:23:54 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.digikala.com/search/category-book/?type[0]=4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4>
{'title': 'کتاب شفای زندگی اثر لوییز هی', 'star': 4.5, 'discounted_percent': 21, 'discounted_price': 50500, 'original_price': 64000, 'discounted_amount': 13500, 'url': 'https://www.digikala.com/product/dkp-137593/کتاب-شفای-زندگی-اثر-لوییز-هی'}
2020-09-01 16:23:54 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.digikala.com/search/category-book/?type[0]=4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4>
{'title': 'کتاب اثر مرکب اثر دارن هاردی نشر نسل نواندیش', 'star': 4.6, 'discounted_percent': 15, 'discounted_price': 32200, 'original_price': 37900, 'discounted_amount': 5700, 'url': 'https://www.digikala.com/product/dkp-1808376/کتاب-اثر-مرکب-اثر-دارن-هاردی-نشر-نسل-نواندیش'}
2020-09-01 16:23:54 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.digikala.com/search/category-book/?type[0]=4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4>
{'title': 'کتاب مادر کافی اثر جی. ا. فرست', 'star': 4.5, 'discounted_percent': 15, 'discounted_price': 49300, 'original_price': 58000, 'discounted_amount': 8700, 'url': 'https://www.digikala.com/product/dkp-38932/کتاب-مادر-کافی-اثر-جی-ا-فرست'}
2020-09-01 16:23:54 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.digikala.com/search/category-book/?type[0]=4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4>
{'title': 'کتاب چهار میثاق اثر دون میگوئل روئیز', 'star': 4.6, 'discounted_percent': 15, 'discounted_price': 15300, 'original_price': 18000, 'discounted_amount': 2700, 'url': 'https://www.digikala.com/product/dkp-37451/کتاب-چهار-میثاق-اثر-دون-میگوئل-روئیز'}
2020-09-01 16:23:54 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.digikala.com/search/category-book/?type[0]=4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4>
{'title': 'کتاب نمی گذارم کسی اعصابم را به هم بریزد اثر آلبرت الیس', 'star': 4.3, 'discounted_percent': 15, 'discounted_price': 39000, 'original_price': 45900, 'discounted_amount': 6900, 'url': 'https://www.digikala.com/product/dkp-38145/کتاب-نمی-گذارم-کسی-اعصابم-را-به-هم-بریزد-اثر-آلبرت-الیس'}
2020-09-01 16:23:54 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.digikala.com/search/category-book/?type[0]=4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4>
{'title': 'کتاب قانون توانگری اثر کاترین پاندر انتشارات آلوس', 'star': 4.5, 'discounted_percent': 68, 'discounted_price': 22100, 'original_price': 70000, 'discounted_amount': 47900, 'url': 'https://www.digikala.com/product/dkp-2359806/کتاب-قانون-توانگری-اثر-کاترین-پاندر-انتشارات-آلوس'}
2020-09-01 16:23:54 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.digikala.com/search/category-book/?type[0]=4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4>
{'title': 'کتاب برتری خفیف اثر جف اولسون', 'star': 4.5, 'discounted_percent': 15, 'discounted_price': 38200, 'original_price': 45000, 'discounted_amount': 6800, 'url': 'https://www.digikala.com/product/dkp-141776/کتاب-برتری-خفیف-اثر-جف-اولسون'}
2020-09-01 16:23:54 [scrapy.core.scraper] ERROR: Spider error processing <GET https://www.digikala.com/search/category-book/?type[0]=4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4> (referer: None)
Traceback (most recent call last):
  File "C:\Users\shima\anaconda3\envs\virtual_workspace\lib\site-packages\scrapy\utils\defer.py", line 102, in iter_errback
    yield next(it)
  File "C:\Users\shima\anaconda3\envs\virtual_workspace\lib\site-packages\scrapy\spidermiddlewares\offsite.py", line 29, in process_spider_output
    for x in result:
  File "C:\Users\shima\anaconda3\envs\virtual_workspace\lib\site-packages\scrapy\spidermiddlewares\referer.py", line 339, in <genexpr>
    return (_set_referer(r) for r in result or ())
  File "C:\Users\shima\anaconda3\envs\virtual_workspace\lib\site-packages\scrapy\spidermiddlewares\urllength.py", line 37, in <genexpr>
    return (r for r in result or () if _filter(r))
  File "C:\Users\shima\anaconda3\envs\virtual_workspace\lib\site-packages\scrapy\spidermiddlewares\depth.py", line 58, in <genexpr>
    return (r for r in result or () if _filter(r))
  File "C:\Users\shima\projects\digikala\digikala\spiders\promotions.py", line 23, in parse
    discounted_percent = int(str(product.xpath(".//div[@class='c-price__discount-oval']/span/text()").get().strip()).replace('٪', ''))
AttributeError: 'NoneType' object has no attribute 'strip'
2020-09-01 16:23:54 [scrapy.core.engine] INFO: Closing spider (finished)
2020-09-01 16:23:54 [scrapy.extensions.feedexport] INFO: Stored xml feed (9 items) in: 99-6-11-12.xml
2020-09-01 16:23:54 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 390,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 81890,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2020, 9, 1, 11, 53, 54, 883694),
 'item_scraped_count': 9,
 'log_count/DEBUG': 10,
 'log_count/ERROR': 1,
 'log_count/INFO': 10,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'spider_exceptions/AttributeError': 1,
 'start_time': datetime.datetime(2020, 9, 1, 11, 53, 53, 939691)}
2020-09-01 16:23:54 [scrapy.core.engine] INFO: Spider closed (finished)

这是我的蜘蛛:

def parse(self, response):
        for product in response.xpath("//ul[@class='c-listing__items']/li"):
            title= product.xpath(".//a[@class='js-product-url']/text()").get()
            star= float(str(product.xpath(".//div[@class='c-product-box__engagement-rating']/text()").get()))
            discounted_percent = int(str(product.xpath(".//div[@class='c-price__discount-oval']/span/text()").get().strip()).replace('٪', ''))
            discounted_price= int(str(product.xpath(".//div[@class='c-price__value-wrapper']/text()").get().strip()).replace(',', ''))
            original_price= int(str(product.xpath(".//div[@class='c-price__value c-price__value--plp']/del/text()").get().strip()).replace(',', ''))
            url= response.urljoin(product.xpath(".//a[@class='js-product-url']/@href").get())
            discounted_amount= original_price-discounted_price
            if star>=3.5 and (discounted_amount>=5000 or discounted_percent>=10):
                yield{
                    'title':title,
                    'star':star,
                    'discounted_percent':discounted_percent,
                    'discounted_price':discounted_price,
                    'original_price':original_price,
                    'discounted_amount': discounted_amount,
                    'url':url
                }

有另一个帖子有类似的主题,但我不明白该怎么做。有人能帮助我解决这个问题吗?
我有另一个问题。上面的日志是什么意思?

2020-09-01 16:23:54 [scrapy.core.engine] DEBUG: Crawled (200) <GET 
https://www.digikala.com/search/category-book/?type[0]= 
4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4> 
(referer: None)

我在www.example.com中更改了ROBOTSTXT_OBEY = Falsesettings.py如果有帮助的话
我还应该做什么来删除这个错误?是因为URL的长度吗?还是什么?
谢谢你

qvk1mo1f

qvk1mo1f1#

discounted_percent = int(str(product.xpath(".//div[@class='c-price__discount-oval']/span/text()").get().strip()).replace('٪', ''))
AttributeError: 'NoneType' object has no attribute 'strip'

这是告诉你discounted_percent里面存储的是NoneType,换句话说,它的值是None。因为None不是字符串,所以没有strip()方法。因此,它会引发AttributeError
这是返回None,至少对于其中一个产品,其他产品似乎都很好:

product.xpath(".//div[@class='c-price__discount-oval']/span/text()").get()

您可能需要在调用.strip()之前检查它是否返回了一个值,或者更好的做法是在其他地方执行所有这些处理。
关于你的另一个问题:

2020-09-01 16:23:54 [scrapy.core.engine] DEBUG: Crawled (200) <GET 
https://www.digikala.com/search/category-book/?type[0]= 
4844&promotion_types[0]=promotion&pageno=1&last_filter=promotion_types&last_value=promotion&sortby=4> 
(referer: None)

这不是问题,它只是一条调试消息,告诉你蜘蛛已经抓取了上面提到的页面。如果你不想看到调试消息,你可以在settings.py中设置LOG_LEVEL='INFO'

ar5n3qh5

ar5n3qh52#

.strip()方法仅适用于字符串,并且某些已解析的元素返回None值。
您可以通过将空字符串指定为 default return value 来解决此问题,如下所示:

.get(default='')

这将得到一个空字符串而不是None对象。请参阅零碎的“Selectors”文档。

相关问题